Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Is there a simple syntax to logically slice a hash?

by LanX (Canon)
on Feb 22, 2013 at 01:50 UTC ( #1020045=note: print w/ replies, xml ) Need Help??


in reply to Is there a simple syntax to logically slice a hash?

define your own hgrep!

$a=key $b=value
DB<142> sub hgrep (&%) { my $f=shift; my @res; local ($a,$b); while ( ($a,$b)= splice(@_,0,2) ) { push @res, $a => $b if $f->() } return @res } DB<143> %h = ( cat=>0, cut=>1, caty=>2 ) => ("cat", 0, "cut", 1, "caty", 2) DB<144> hgrep { $a =~ /^cat/ } %h => ("cat", 0, "caty", 2) DB<145> hgrep { $b>1 } %h => ("caty", 2)

Cheers Rolf

UPDATE: see Hash::MostUtils for a similar approach.


Comment on Re: Is there a simple syntax to logically slice a hash?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2015-07-28 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls