> My problem is this: > > I'm taking the results from a hash search (that determines if a particular > student name and password combination is valid) and passing it to another > hash search (which is part of an if loop), which is supposed to print out > the assignments for the student. That works with one small problem, it > reprints the students name under the line with the assignments. The other > problem is that if I repeat the loop, it doubles the results, so I get two > identicle lines. > > Here's my search code. Any suggestions on what I'm doing wrong would be > greatly appreciated. > > $nk is the password, $search is the student name searched out previously. > > while (@n = each %assignments) { > if (grep /($nk)/, @n) { > $keys[++$#keys] = $n[0]; > } > }
Hmm... looks buggy... it means: while each %assignments if greps "$nk" in key or value then stick key at end of @keys Probably you want: while (@n = each %assignments) { if (grep $nk, $n[1]) { $keys[++$#keys] = $n[0]; } } Since you are checking both the key and the value, yet they are usually totally different things... they should be. This also implies that the grep isn't appropriate either. So, with various style changes: while (my ($user, $passwd) = each %assignments) { push @keys, $users if $passwd eq $newpass } I'm not sure exactly what you are doing, but please read: perldoc -f push > if (@keys) { > print "Your assignments: \n"; > foreach $password (sort { $b cmp $a } @keys) { > print " $search, $assignments{$password}\n"; > } print "Your assignments:\n"; print " $search, $assignments{$_}\n" foreach sort { $b cmp $a } @keys; > } else { I'll overgeneralise by saying that Perl programmers don't use "cuddled elses". They write that like: } else { Usually, it's just Larry Wall's influence on the holy war of indent style :) See: perldoc perlstyle for details. > print "That password doesn't exist.\n"; > } Jonathan Paton __________________________________________________ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]