If case-insensitive lookup (and storage) of the usernames is the
idea, then you could apply the same case transformation on the hash key
(all uppercase, all lowercase, etc.) prior to storing the entry, and
then similarly upon lookup, e.g.
...
$name_hash{uc $username} = $password;
...
while(<STDIN>) {
chomp;
exit if $_ eq 'exit';
my $name = uc $_;
if (exists $name_hash{$name}) {
print "The password for $name is $name_hash{$name}, please ent
+er another name to search\n";
} else {
print "I'm sorry, there is no one here by that name, please tr
+y another name or type exit to end.\n";
}
}
This would simplify things by taking advantage of hashes' key
feature (pun intended) to allow a direct lookup, without having to loop
over all of its entries.
Note that this suggestion differs slightly from what your code
does, in that yours will (a) find the name even if only a substring of
it has been specified as the lookup string, and (b) will return
multiple matches if the hash happens to be storing several
differently spelled variants (Name, name, NAME, ...) of the same username... I'm not sure
whether you actually need those features, so I'm mentioning this
simplification just in case my suspicion should hold true that you
don't... :)
|