For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2005 > Replacing a Bracketed String with "N"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Replacing a Bracketed String with "N"
Edward Wijaya

2005-11-22, 6:57 pm

Hi,

Is there a fast way to replace string like this:


$str1 = "ATC[TG]CC";
# into
$ans_str1 = "ATCNCC";

#and

$str2 ="ATC[TG]CCGC[ACTG]";
#into
$ans_str2="ATCNCCGCN";


Thus, the position of the bracket can be in any positions with any number,
and number of strings enclosed by the bracket can consist up to 4 bases, i.e. [ATCG].

--
Regards,
Edward WIJAYA
SINGAPORE


Jeff 'japhy' Pinyan

2005-11-22, 6:57 pm

On Nov 22, Edward Wijaya said:

> $str1 = "ATC[TG]CC";
> # into
> $ans_str1 = "ATCNCC";
>
> $str2 ="ATC[TG]CCGC[ACTG]";
> #into
> $ans_str2="ATCNCCGCN";


The simplest way I can think of is

$string =~ s/\[[ACTG]+\]/N/g;

which is very explicit: it replaces a '[' followed by A's, C's, T's,
and/or G's, followed by a ']' with 'N'.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart
Jose J. Cintron

2005-11-22, 6:57 pm

Try this one...

$str =3D "AABBE[TH]AODD[AAAB]AQA";
$str =3D~ s/\[[A-Z]*\]/N/g;
print "$str\n\n";


+------------------------------------------
| Jos=E9 J. Cintr=F3n - <jcintron@mitre.org>
+------------------------------------------=20

> -----Original Message-----
> From: Edward Wijaya [mailto:ewijaya@singnet.com.sg]=20
> Sent: Tuesday, November 22, 2005 10:22
> To: beginners@perl.org
> Subject: Replacing a Bracketed String with "N"
>=20
> Hi,
>=20
> Is there a fast way to replace string like this:
>=20
>=20
> $str1 =3D "ATC[TG]CC";
> # into
> $ans_str1 =3D "ATCNCC";
>=20
> #and
>=20
> $str2 =3D"ATC[TG]CCGC[ACTG]";
> #into
> $ans_str2=3D"ATCNCCGCN";
>=20
>=20
> Thus, the position of the bracket can be in any positions=20
> with any number, and number of strings enclosed by the=20
> bracket can consist up to 4 bases, i.e. [ATCG].=20
>=20
> --
> Regards,
> Edward WIJAYA
> SINGAPORE
> =20
> =20
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org For=20
> additional commands, e-mail: beginners-help@perl.org=20
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>=20

Purl Gurl

2005-11-22, 6:57 pm

Edward Wijaya wrote:

> $str1 = "ATC[TG]CC";
> # into
> $ans_str1 = "ATCNCC";


> $str2 ="ATC[TG]CCGC[ACTG]";
> # into
> $ans_str2="ATCNCCGCN";


> Thus, the position of the bracket can be in any positions with any number,
> and number of strings enclosed by the bracket can consist up to 4 bases, i.e. [ATCG].


A presumption is you want to replace [any characters] with an N character,
regardless of content enclosed in brackets, and your string will ALWAYS
contain at least one [any characters] inclusion.

If [any characters] is not present, do not use this method or code for this circumstance.

Work towards writing articles which are clear, concise and coherent.

Purl Gurl

#!perl

$string = "ATC[TG]CCGC[ACTG]";

do
{ substr($string, index($string, "["), (index($string, "]") - index($string, "[") + 1), "N"); }
until (index($string, "[") == -1);

print $string;
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com