Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: grep keys in hash and retrieve values

by Anonymous Monk
on Mar 16, 2012 at 10:07 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found