in reply to when do i know that the iterator for a hash was reseted
I see no need to use each in this case:
sub find_in { my ($data_ref, $string) = @_; my $result = 'no result'; my ($key) = grep /^$string$/i, keys %$data_ref; $result = $$data_ref{$key}; return $result; }
Update: condensed and with a modified data set:
sub find_in { my ($data_ref, $string) = @_; return $$data_ref{ (grep /^$string$/i, keys %$data_ref)[0] || '' } + || 'no result'; } sub look_for_data { my %hash = ( A => 'axxx' , B => 'bxxx' , C => 'cxxx' , D => 'dxxx' +); print "d: " . find_in(\%hash , 'd') . "\n"; print "A: " . find_in(\%hash , 'A') . "\n"; print "B: " . find_in(\%hash , 'B') . "\n"; print "Z: " . find_in(\%hash , 'Z') . "\n"; } look_for_data(); __END__ d: dxxx A: axxx B: bxxx Z: no result
Update 2: Added ^ and $ anchors as per jhourcle advice.
Update 3: Same again.
--
David Serrano
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: when do i know that the iterator for a hash was reseted
by jhourcle (Prior) on Apr 20, 2006 at 15:12 UTC | |
by Anonymous Monk on Apr 20, 2006 at 15:38 UTC | |
|
Re^2: when do i know that the iterator for a hash was reseted
by Anonymous Monk on Apr 20, 2006 at 13:27 UTC |