Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

problems with converting a string character by character

by cburger (Acolyte)
on Feb 02, 2011 at 00:53 UTC ( [id://885632]=perlquestion: print w/replies, xml ) Need Help??

cburger has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a script which replaces one letter with another for nucleotide bases. e.g. A with T. when I write:

for example I want to convert this string:

AAGCTT

with:

TTCGAA

I wrote something like:

while(/(.)/g) { my $letter= uc $1; print $letter; my $len=length($letter); my $sub=""; if ($letter=="A") { $sub="T";} elsif ($letter=="T") { $sub="A";} elsif ($letter=="G") { $sub="C";} elsif ($letter=="C") { $sub="G";} print " $len\t$sub\n"; } # close while

the replacement is not correct and I get for the print out:

A 1 T A 1 T G 1 T C 1 T T 1 T T 1 T

what is happening? why is sub always T and not changing? I dont see the problem with the lines.. thanks for the help!

thanks everyone! I could figure out the problem now. Indeed the eq sign mix up was the problem, and I wasnt aware of the tr// possibility. Painful learning from sloppiness...Thanks again for the help perlmonks!

Replies are listed 'Best First'.
Re: problems with converting a string character by character
by toolic (Bishop) on Feb 02, 2011 at 01:02 UTC
    If you use strict and warnings, you will see these warnings:
    Argument "A" isn't numeric in numeric eq (==)
    You need to use string equality. Change == to eq
    use warnings; use strict; $_ = 'AAGCTT'; while (/(.)/g) { my $letter = uc $1; print $letter; my $len = length($letter); my $sub = ""; if ( $letter eq "A" ) { $sub = "T"; } elsif ( $letter eq "T" ) { $sub = "A"; } elsif ( $letter eq "G" ) { $sub = "C"; } elsif ( $letter eq "C" ) { $sub = "G"; } print " $len\t$sub\n"; } # close while __END__ A 1 T A 1 T G 1 C C 1 G T 1 A T 1 A
    Another way to do it is with a hash:
    my %conv = ( A => 'T', T => 'A', G => 'C', C => 'G', ); $_ = 'AAGCTT'; while (/(.)/g) { my $letter = uc $1; print $letter; my $len = length($letter); print " $len\t$conv{$letter}\n"; }
Re: problems with converting a string character by character
by Anonyrnous Monk (Hermit) on Feb 02, 2011 at 00:59 UTC

    Use eq in place of ==.

    But maybe you're rather looking for tr/ATGC/TACG/.  See tr.

Re: problems with converting a string character by character
by jethro (Monsignor) on Feb 02, 2011 at 01:09 UTC

    By the way, there is already a builtin function for this. Your script could be written as

    $_= uc($_); tr/ATGC/TACG/;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://885632]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2025-03-22 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.