Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: problems with converting a string character by character

by toolic (Bishop)
on Feb 02, 2011 at 01:02 UTC ( [id://885634]=note: print w/replies, xml ) Need Help??


in reply to problems with converting a string character by character

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"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2025-11-07 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (65 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.