in reply to File Reading and Hash Accessing that isn't doing what you'd expect.
Anyway, one problem with your code is that you iterate over a hash, abort in the middle and don't reset the iterator.
Iterating over a hash is a bad idea anyway - just store it the other way round (ie turn the values into keys and the other way round), and do a lookup:
# assuming you have no duplicate values: my %mapkey = reverse %keymap; sub findCharacterAsKey { my $loebnerLetter = shift; if (exists $mapkey{$lobnerLetter}) { return $mapkey{$lobnerLetter}; } else { warn "Could not resolve '$loebnerLetter'"; } }
Since you have to do the inversion of the hash only once, it'll be much faster and correct this way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: File Reading and Hash Accessing that isn't doing what you'd expect.
by Chris_Stevens (Initiate) on Jun 01, 2010 at 14:32 UTC | |
by moritz (Cardinal) on Jun 01, 2010 at 14:43 UTC |