Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by TomDLux (Vicar)
on Feb 21, 2013 at 16:29 UTC ( #1019976=note: print w/replies, xml ) Need Help??


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

Using the perl debugger, for speed and convenience ...

# Lets create a hash that has some similar keys and some unsimilar key +s %h1 = ( cat => 1, dog=> 2, catalog => 3, doggerel => 4 } x \%h1 0 HASH(0x7f9cb41aaad0) 'cat' => 1 'catalog' => 3 'dog' => 2 'doggerel' => 4 # See ALL the keys ... x keys %h1 0 'cat' 1 'doggerel' 2 'catalog' 3 'dog' # grep allows us to filter that list x grep { /cat/ } keys %h1 0 'cat' 1 'catalog' # You access a single hash element with $h{$key} # You can access multiple elements, called a slice, # using an array or list of keys. In this case you have to put # a '@' sigil in front, rather than a '$', to indicate #you're expecting multiple values x @h1{grep { /cat/ } keys %h1} 0 1 1 3 # So 'cat' -like keys have the values, '1' and '3'. # If you wanted to assign those to the keys in # another hash, I would save the set of keys in a temp: @a = grep { /cat/ } keys %h1 @b{@a} = @h1{@a} DB<14> x \%b 0 HASH(0x7f9cb41bd0e8) 'cat' => 1 'catalog' => 3

The last bit is saying, create a new hash called 'b' ( we know it's a hash because of how it's elements are being referenced with curly braces ), and create elements for each value in @a, assigning the elements extraced from %h1. You COULD repeat the 'grep' part, once in the LHS and once on the RHS of the assignment, to really get a one-line assignment, but that's messy. While you don't know the order of the elements, it would be the same on each side, but if the set is huge, you don't want to go through all that .. and it's a maintainence problem, and hard to verify the two are identical; so I would split it into two parts.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re^2: Is there a simple syntax to logically slice a hash?
by BrowserUk (Pope) on Feb 21, 2013 at 16:41 UTC

    Ouch!. That creates 5 unneccesary temporary lists and an unneccesary temporary array:

    #T1 L2 L1 @a = grep { /cat/ } keys %h1 # L5 L4 L3 @b{@a} = @h1{@a}

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019976]
help
Chatterbox?
[perldigious]: Also coincidentally, I'm currently in the middle of a winter storm that has dropped 1 foot of snow and is expected to drop another. I might have to adapt this for customers/vendors who come visit and put it at the beggining of every presentation I give.
[Discipulus]: the photo was removed... Cyrano de Berjerac was not of the same idea..
[stevieb]: perldigious: video near our hq in Montreal. Here in Calgary, it's -28c... we've only got 3" of snow so far, but there are several feet in the mountains already
[perldigious]: Snowshoeing time stevieb? I need to buy a pair so I can walk my German Shepherd properly through the country.
[stevieb]: yeah, I snowshoe a fair amount in the mountains. I also use spikes similar to these very often as well
[perldigious]: Discipulus: alternate link

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2016-12-06 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (112 votes). Check out past polls.