这是一个家庭作业。我不是在寻找“使其工作的代码”,而是在寻找我的逻辑错误的正确方向。
use strict;
use warnings;
#rot13 sub for passwords
sub rot13{
my $result;
chomp(my $input = <STDIN>);``
# all has to be lower case
my $lower = lc $input;
my $leng = length $lower;
for(my $i = 0; $i < $leng; $i++){
my $temp = substr ($lower,$i,1);
my $con = ord $temp;
if($con >= '55'){
if($con >= '110'){
$con -= 13;
}
else{
$con += 13;
}
}
$result = $result . chr $con;
}
return $result;
};
#opening a file specified by the user for input and reading it
#into an array then closing file.
open FILE, $ARGV[0] or die "cannot open input.txt";
my @input = <FILE>;
close FILE;
my (@username,@password,@name,@uid,@shell,@ssn,@dir,@group,@gid);
my $ui = 100;
foreach(@input){
my ($nam, $ss, $gro) = split ('/', $_);
chomp ($gro);
$nam= lc $nam;
我创建了一个哈希,因此我可以使用 exists 函数,然后使用该函数,如果它确实存在,则进入下一轮循环。我觉得我错过了一些东西。
my %nacheck;
if( exists ($nacheck { '$nam' } )){
next;
}
$nacheck{ "$nam" } = 1;
while (my ($key, $value) = each %nacheck){
print "$key => $value\n";
}
所有这些现在都有效,但是任何关于如何做得更好的提示都会被欣赏
my($unf, $unm, $unl) = split (/ /, $nam);
$unf = (substr $unf,0,1);
$unm = (substr $unm,0,1);
$unl = (substr $unl,0,1);
my $un = $unf . $unm . $unl;
if(($gro) eq "faculty"){
push @username, $un;
push @gid, "1010";
push @dir, "/home/faculty/$un";
push @shell, "/bin/tcsh";
}
else{
my $lssn = substr ($ss,7,4);
push @username, $un . $lssn;
push @gid, "505";
push @dir, "/home/student/$un";
push @shell, "/bin/bash";
}
#pushing results onto global arrays to print out later
push @ssn, $ss;
my $pass = rot13;
push @password, $pass;
push @name, $nam;
push @uid, $ui += 1;
}
#printing results
for(my $i = 0; $i < @username; $i++){
print
"$username[$i]:$password[$i]:$uid[$i]:$gid[$i]:$name[$i]:$dir[$i]:$shell[$i]\n";
}