in reply to Perl hash comparison
Perhaps I'm misunderstanding your issue's description, but it sounds like the following: given a set of characters and a hash key, return (chars in key / 5) * $hash{key}. If this is the case, perhaps the following will work for you:
use Modern::Perl; my %hash = ( ABCDE => 12, FGHIJ => 7, KLMNO => 2 ); say getMatchResult( 'AC', 'ABCDE' ); sub getMatchResult { my ( $find, $key ) = @_; my $matches = () = $key =~ /[$find]/ig; ( $matches / 5 ) * $hash{$key}; }
Output:
4.8
Hope this helps!
Update: Thanks to hmb for suggesting the goatse operator (and character class). It timed faster than my iterative solution w/regex.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl hash comparison
by hbm (Hermit) on Aug 17, 2012 at 00:37 UTC | |
by Kenosis (Priest) on Aug 17, 2012 at 01:29 UTC |