打印特定的哈希值perl

时间:2014-03-06 10:12:17

标签: perl

我正在尝试为具有相应字母的数字打印哈希值,我是新手,我不知道如何让我的条件语句遍历我在一行用户输入中输入的每个值。我正在尝试制作密码。


#!/usr/bin/perl
print "Enter the numbers for the 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 a jersey number we have stats on.\n";}
chomp($num = <>);
}

2 个答案:

答案 0 :(得分:0)

这不是我最好的Perl,但更接近你的想法:

#!/usr/bin/perl
use strict;
use warnings;

my %num_to_letter=(
    4  => "A",
    9  => "B",
    17 => "C"
);

my $num;

print "Enter the numbers for the corresponding letters\n";

$num=0;
while ($num >= 0 && $num <= 27 || $num == 'c'){
chomp( $num = <>);
   if(defined $num_to_letter{$num}){
      print $num_to_letter{$num},"\n";
   }else{
      print "Outta luck\n";
   }
}

答案 1 :(得分:0)

您必须提高编码技能。

我想你想要一个perl脚本(即crypt.pl),当被调用时,就像这样

$ perl crypt.pl 1 4 6 12 3 6 7 ..

它返回

K A H O T H F ..

一个例子可能是以下一个:

#!/usr/bin/perl -w # ALWAYS start
use warnings; # your scripts with
use strict; # these three lines

my %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 => " "
); # this could be a better way of
# declaring and defining an hash content

foreach my $num (@ARGV) {
  print $num_to_letter{$num} . " ";
} # here is how you get each value

也许您想添加一些输入验证。