printing specific hash values perl -
i trying print hash values numbers have corresponding letters, new @ this, don't know how conditional statements run through each value put in on single line of user input. trying make cryptogram.
#!/usr/bin/perl print "enter numbers corresponding letters\n"; chomp( $num = <>); while ($num >= 0 && $num <= 27 || $num == 'c'){ %num_to_letter; $num_to_letter{"4"} = "a"; $num_to_letter{"9"} = "b"; $num_to_letter{"17"} = "c"; $num_to_letter{"5"} = "d"; $num_to_letter{"13"} = "e"; $num_to_letter{"7"} = "f"; $num_to_letter{"21"} = "g"; $num_to_letter{"6"} = "h"; $num_to_letter{"23"} = "i"; $num_to_letter{"15"} = "j"; $num_to_letter{"1"} = "k"; $num_to_letter{"20"} = "l"; $num_to_letter{"26"} = "m"; $num_to_letter{"19"} = "n"; $num_to_letter{"12"} = "o"; $num_to_letter{"2"} = "p"; $num_to_letter{"27"} = "q"; $num_to_letter{"11"} = "r"; $num_to_letter{"25"} = "s"; $num_to_letter{"3"} = "t"; $num_to_letter{"22"} = "u"; $num_to_letter{"14"} = "v"; $num_to_letter{"18"} = "w"; $num_to_letter{"8"} = "x"; $num_to_letter{"24"} = "y"; $num_to_letter{"16"} = "z"; $num_to_letter{"10"} = " "; if ($num == 4){ print "$num $num_to_letter{$num}"; print $num_to_letter{"4"} = "a"; } elsif ($num == 9){ print "$num $num_to_letter{$num}"; print $num_to_letter{"9"} = "b"; } elsif ($num == 17 ){ print $num $num_to_letter{$num}; print $num_to_letter{"17"} = "c"; } elsif ($num == 5){ print $num $num_to_letter{$num}; print $num_to_letter{"5"} = "d"; } elsif ($num == 13 ){ print $num $num_to_letter{$num}; print $num_to_letter{"13"} = "e"; } elsif ($num == 7 ){ print $num $num_to_letter{$num}; print $num_to_letter{"7"} = "f"; } elsif ($num == 21){ print $num $num_to_letter{$num}; print $num_to_letter{"21"} = "g"; } elsif ($num == 6){ print $num $num_to_letter{$num}; print $num_to_letter{"6"} = "h"; } elsif ($num == 23){ print $num $num_to_letter{$num}; print $num_to_letter{"23"} = "i"; } elsif ($num == 15){ print $num $num_to_letter{$num}; print $num_to_letter{"15"} = "j"; } elsif ($num == 1){ print $num $num_to_letter{$num}; print $num_to_letter{"1"} = "k"; } elsif ($num == 20){ print $num $num_to_letter{$num}; print $num_to_letter{"20"} = "l"; } elsif ($num == 26){ print $num $num_to_letter{$num}; print $numto_letter{"26"} = "m"; } elsif ($num == 19){ print $num $num_to_letter{$num}; print $num_to_letter{"19"} = "n"; } elsif ($num == 12 ){ print $num $num_to_letter{$num}; print $num_to_letter{"12"} = "o"; } elsif ($num == 2){ print $num $num_to_letter{$num}; print $num_to_letter{"2"} = "p"; } elsif ($num == 27){ print $num $num_to_letter{$num}; print $num_to_letter{"27"} = "q"; } elsif ($num == 11){ print $num $num_to_letter{$num}; print $num_to_letter{"11"} = "r"; } elsif ($num == 25){ print $num $num_to_letter{$num}; print $num_to_letter{"25"} = "s"; } elsif ($num ==3){ print "$num $num_to_letter{$num}"; $num_to_letter{"3"} = "t"; } elsif ($num == 22){ print $num $num_to_letter{$num}; $num_to_letter{"22"} = "u"; } elsif ($num == 14){ print $num $num_to_letter{$num}; $num_to_letter{"14"} = "v"; } elsif ($num == 18){ print $num $num_to_letter{$num}; $num_to_letter{"18"} = "w"; } elsif ($num == 8){ print $num $num_to_letter{$num}; $num_to_letter{"8"} = "x"; } elsif ($num == 24){ print $num $num_to_letter{$num}; $num_to_letter{"24"} = "y"; } elsif ($num == 16){ print $num $num_to_letter{$num}; $num_to_letter{"16"} = "z"; } elsif ($num == 10){ print $num $num_to_letter{$num}; $num_to_letter{"10"} = " "; } else{ print "you didn't enter jersey number have stats on.\n";} chomp($num = <>); }
you have improve coding skill.
i suppose want perl script (i.e. crypt.pl
) when called, this
$ perl crypt.pl 1 4 6 12 3 6 7 ..
it returns
k h o t h f ..
an example following one:
#!/usr/bin/perl -w # start use warnings; # scripts use strict; # these 3 lines %num_to_letter = ( 4 => "a", 9 => "b", 17 => "c", 5 => "d", 13 => "e", 7 => "f", 21 => "g", 6 => "h", 23 => "i", 15 => "j", 1 => "k", 20 => "l", 26 => "m", 19 => "n", 12 => "o", 2 => "p", 27 => "q", 11 => "r", 25 => "s", 3 => "t", 22 => "u", 14 => "v", 18 => "w", 8 => "x", 24 => "y", 16 => "z", 10 => " " ); # better way of # declaring , defining hash content foreach $num (@argv) { print $num_to_letter{$num} . " "; } # here how each value
maybe add input validation.
Comments
Post a Comment