Click here to get back home

Help: Replace Help

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help: Replace Help Amy Lee 05-01-2008
Posted by Jürgen Exner on May 1, 2008, 8:50 am
Please log in for more thread options

>>seq1
>ACGU
>>seq2
>GUACCGU
>
>And I wanna replace A to C, C to A, G to U, U to G. So from my point the
>reversed file should be viewed like this.
>
>>seq1
>CAUG
>>seq2
>UGCAAUG
>
>This is my codes.
[4 individual s///]

>So how to solve this kind of order problem? I suppose that the replacement
>must process at the same time.

Long-winded option: replace A with some temporary value, e.g. X, then C
to A, then X to C. And then the same for G and U.

Much better option: use tr{}{}

        tr ;

jue

Posted by Amy Lee on May 1, 2008, 9:02 am
Please log in for more thread options
On Thu, 01 May 2008 12:50:48 +0000, Jürgen Exner wrote:

>
>>>seq1
>>ACGU
>>>seq2
>>GUACCGU
>>
>>And I wanna replace A to C, C to A, G to U, U to G. So from my point the
>>reversed file should be viewed like this.
>>
>>>seq1
>>CAUG
>>>seq2
>>UGCAAUG
>>
>>This is my codes.
> [4 individual s///]
>
>>So how to solve this kind of order problem? I suppose that the replacement
>>must process at the same time.
>
> Long-winded option: replace A with some temporary value, e.g. X, then C
> to A, then X to C. And then the same for G and U.
>
> Much better option: use tr{}{}
>
>         tr ;
>
> jue
Thank you very much. I've solved my problem. And could you tell me what
{} stands for?

Thank you again~

Amy

Posted by RedGrittyBrick on May 1, 2008, 9:28 am
Please log in for more thread options
Amy Lee wrote:
> On Thu, 01 May 2008 12:50:48 +0000, Jürgen Exner wrote:
>
>>
>>> I wanna replace A to C, C to A, G to U, U to G.
>>
>>         tr ;
>>
> could you tell me what {} stands for?
>

{} stands for {}

They are just used to group the characters to be replaced and their
replacements.

The following are all equivalent

tr/ACGU/CAUG/;
tr!ACGU!CAUG!;
tr-ACGU-CAUG-;
tr.ACGU.CAUG.;

tr;
tr(ACGU)(CAUG);
tr[ACGU][CAUG];
tr<ACGU>(CAUG);

Perl lets you use almost any character as a delimiter/separator for the
two groups of characters, you can instead use any of a few types of
bracket or brace like characters to group the two sets of characters.

Choose whatever characters make the code clearest to readers. The oldest
form is the first shown above but people can use one of the other forms
for greater clarity if, for example, they need to translate '/' to
something else.

--
RGB

Posted by Jo on May 1, 2008, 10:11 am
Please log in for more thread options
RedGrittyBrick said:
> The following are all equivalent
> tr/ACGU/CAUG/;
> ...
> tr[ACGU][CAUG];

I'd like to add that whitespace is allowed also. This can help writing
readable code:

tr [ACGU]
[CAUG];



Posted by Jürgen Exner on May 1, 2008, 9:36 am
Please log in for more thread options
>On Thu, 01 May 2008 12:50:48 +0000, Jürgen Exner wrote:
>> Much better option: use tr{}{}
>>
>>         tr ;
>>
> And could you tell me what {} stands for?

Hmmmm, what do you mean? It's just curly brackets or braces, see
http://en.wikipedia.org/wiki/Brackets#Uses_of_.E2.80.9C.7B.E2.80.9D_and_.E2.80.9C.7D.E2.80.9D

And maybe 'perldoc perlop', section 'Quotes and quote-like Operators'.

jue

Similar ThreadsPosted
s/$match/$replace/ fails when $replace has backreferences September 5, 2005, 7:05 pm
Replace with nothing May 12, 2005, 7:54 am
HOW TO replace ' but not ?' October 27, 2004, 4:30 pm
search & replace September 7, 2004, 12:27 pm
search and replace help April 22, 2005, 2:11 am
searc and replace April 22, 2005, 3:10 am
search and replace September 2, 2005, 6:12 am
match 1/2/3, replace with a/b/c October 15, 2005, 10:30 am
Search and Replace December 6, 2005, 3:41 am
Search and Replace May 24, 2006, 8:11 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap