Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Is there a way to switch a hash values <> keys with a slice?

by Fletch (Bishop)
on Apr 19, 2022 at 14:04 UTC ( [id://11143088]=note: print w/replies, xml ) Need Help??


in reply to Is there a way to switch a hash values <> keys with a slice?

For one thing you're committing a thinko in that a hash is by definition unordered (unless you do some tie magic under the hood) so your destination is misnamed. That being said since keys and values will return things in the same order so long as you don't change the underlying hash you can swap things the obvious way.

main::(-e:1): 0 DB<1> %foo = qw( a b c d ) DB<2> @bar{ values %foo } = keys %foo DB<3> x \%bar 0 HASH(0x7fb23c8b94b8) 'b' => 'a' 'd' => 'c'

The cake is a lie.
The cake is a lie.
The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-28 13:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found