in reply to char substitution without regexes

for (my $i = length($string); $i>= 0 ; $i--){ print $hash{ substr($string, $i, 1) }; }

Replies are listed 'Best First'.
Re^2: char substitution without regexes
by Soul Singin' (Initiate) on Oct 02, 2010 at 16:12 UTC

    Here's a variant on the above, but syntactically clearer:

    #!/usr/bin/env perl use strict ; use warnings ; ## DNA string my $string = "aaaggctt"; ## syntactically clear hash my %hash = ( "a" => "t" , "g" => "c" , "c" => "g" , "t" => "a" ); ## print the DNA string and reverse complement ## add some extra spacing to make it look pretty print "\n" ; print "\t The DNA string is: " . $string . "\n" ; print "\tThe reverse complement is: " ; ## go through the string one element at a time ## and print reverse complement for my $i (0..length($string)-1) { print $hash{ substr($string, $i, 1) } ; } ## finish with a little extra spacing print "\n\n" ;