CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [ssampath123] match part of a string in values of a hash: Edit Log



BillKSmith
Veteran

Nov 8, 2011, 7:15 PM


Views: 754
Re: [ssampath123] match part of a string in values of a hash

It appears that you want "Found" printed only once even if a match occurs in several values.


Code
 use strict;  
use warnings;
use List::MoreUtils qw( any );
my %f1H = (
key1 => 'I love basketball',
key2 => 'I love baseball',
key3 => 'I love football very much',
key4 => 'I watch Monday night footbal every week',
);

my $val = qr /football/;
if (any {$val} values %f1H) {
print "Found\n" ;
}



You can replace 'any' with 'scalar grep' if you do not want to use the module. I prefer 'any' because it documents your intention.



I see that you are using an integer (10) as a hash key. If all your keys are small integers, you probably should be using an array rather than a hash.
Good Luck,
Bill

(This post was edited by BillKSmith on Nov 8, 2011, 7:20 PM)


Edit Log:
Post edited by BillKSmith (Veteran) on Nov 8, 2011, 7:20 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives