Jolinar Of Malkshur wrote:
>
> Ok, this still deals with that Perl class I'm taking, so be warned. And
> please don't laugh at my coding, I'm very new to Perl, so it's bound to look
> pathetic to those of you who have been doing this longer.
>
> 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];
^^^^^^^^ ^^^^^
You should probably use push() instead of $keys[++$#keys] ($keys[@keys]
would also work). Since you are only using the keys why are you
comparing against the values as well?
> }
> }
>
> if (@keys) {
> print "Your assignments: \n";
> foreach $password (sort { $b cmp $a } @keys) {
> print " $search, $assignments{$password}\n";
> }
> } else {
> print "That password doesn't exist.\n";
> }
To determine whether a hash contains a certain key use the exists()
funtion:
my %hash = ( one => 'first', two => 'second', three => 'third' );
for my $key ( qw/two fred/ ) {
if ( exists $hash{ $key } ) {
print "The key $key exists\n";
}
else {
print "The key $key does not exist\n";
}
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]