Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: grep keys in hash and retrieve values

by Anonymous Monk
on Mar 16, 2012 at 10:07 UTC ( #959951=note: print w/ replies, xml ) Need Help??


in reply to grep keys in hash and retrieve values

I think my $matching_key, is returning the number of keys that match the grep and not the actual hash key.

Then you should write a program to check

$ perl -le " print scalar grep /./, qw/ a b c /; " 3

Yup, grep in scalar context returns the number of matches.

Now in list context it returns a list :)

$ perl -le " print for grep /./, qw/ a b c /; " a b c

So to get the first from the list, we use parens

$ perl -le " ( $foo ) = grep /./, qw/ a b c /; print $foo " a $ perl -le " print( ( grep /./, qw/ a b c / )[0] ); " a

See Tutorials: Context in Perl: Context tutorial

And see again Re: help with loop and start making functions, your program is very hard to read :)


Comment on Re: grep keys in hash and retrieve values
Select or Download Code
Re^2: grep keys in hash and retrieve values
by AWallBuilder (Beadle) on Mar 16, 2012 at 10:40 UTC

    thank you. yes the context tutorial will help. Now I can retrieve the value, but only for exact matches of the keys. But I want a match if it contains hostB I also tried. $regex=qr/$hostB*/ and $regex=qr/^$hostB/ I also read through a few regular expression tutorials and am even more confused.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://959951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-12-17 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (40 votes), past polls