Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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 :)

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://959951]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-06-22 02:50 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.