On May 22, Mark on GCI Server said:
>open(TESTER, "<password.dat") or die "File cannot be opened.\n";
>print "Enter a username: ";
>$input = <STDIN>;
You need to chomp $input, since it has a newline at the end.
>$x = 0;
>$y = 0;
>$w = 0;
>$z = 1;
>if (<TESTER> ne "") {
That reads a line (and it is lost forever).
> while($line = <TESTER>) {
> chomp($line);
> ($stuname, $stupasswd) = split(/,/,$line);
> @students[$x,$y] = ("$stuname");
> @students[$w,$z] = ("$stupasswd");
2-d arrays do not work this way in Perl. You need to say:
$students[$x][$y] = $stuname;
$students[$x][$z] = $stupasswd;
The $w variable here is useless. Doing @array[$x,$y] is the same as
($array[$x], $array[$y]) -- that is, you get two elements from the array.
> $x = $x + 1;
> $w = $w + 1;
> print @students[$x,$y];
> };
>
>if ($stuname <=> $input) {
> print "Username is $stuname and their student number is
>$stupasswd\n";
>} else {
> print "Unauthorized user $input, please check your spelling.\n";
>}
>
>close(TESTER);
>}
You don't need an array here at all, and you probably don't need a hash.
print "Enter a username: ";
chomp(my $input = <STDIN>);
open TESTER, "< password.dat" or die "can't read password.dat: $!";
while (<TESTER>) {
chomp;
my ($u, $p) = split /,/;
if ($u eq $input) {
print "Username: $u\n";
print "Password: $p\n";
last; # this stops reading from the file
}
}
close TESTER;
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **