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

Re^2: inverting hash / grouping values

by LanX (Canon)
on Sep 27, 2013 at 15:13 UTC ( #1055998=note: print w/ replies, xml ) Need Help??


in reply to Re: inverting hash / grouping values
in thread inverting hash / grouping values

> Have you looked at the safe_reverse function in Hash::MoreUtils?

Yes I looked - but I didn't install to try ... hmm thanks!

IMHO the documentation is quite misleading ...

Returns safely reversed hash (value, key pairs of original hash). If no BLOCK is given, following routine will be used:

(value,key pairs ???)

... and better examples showing in and output are lacking.

After looking at it I was unhappy about the blocked namespace, a module called Hash::MoreUtils could offer much more.

Cheers Rolf

( addicted to the Perl Programming Language)

update

After diving into the code, it looks like safe_reverse will not represent unique values as 1-element-arrays but as scalars.

update

This partly explains my confusion, this function works like a normal reverse if all values are unique.

DB<100> use Hash::MoreUtils qw/safe_reverse/ DB<101> @h{a..c}=1..3 => (1, 2, 3) DB<102> safe_reverse \%h => (1, "a", 3, "c", 2, "b") DB<105> @h{a..c}=(1,1,2) => (1, 1, 2) DB<106> safe_reverse \%h => (1, ["a", "b"], 2, "c")

to be able to have the same effect like invert in the OP one needs to write

DB<110> safe_reverse sub { my ($k, $v, $r) = @_; return [ @{$r->{$v} +}, $k ] },\%h => (1, ["a", "b"], 2, ["c"])


Comment on Re^2: inverting hash / grouping values
Select or Download Code
Re^3: inverting hash / grouping values
by marinersk (Chaplain) on Sep 27, 2013 at 15:16 UTC
    I'm confused by your question. I have seen a key and its value frequently refered to as a key/value pair.

    Do I misunderstand you?

      > I have seen a key and its value frequently refered to as a key/value pair.

      the result of this inversion operation is a HoA  { key1 => [ value11, value12], key2 => [value21],  ...}

      And neither

      • a hash with key/value pairs { key1 =>value1 , key2 => value2, ...}
      • a list with key/value pairs ( key1, value1, key2, value2 ,...)
      • a list of arrays with pairs (tuples) ( [key1, value1], [key2,value2], ... )

      IMHO all better interpretations.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re^3: inverting hash / grouping values
by Anonymous Monk on Sep 27, 2013 at 18:00 UTC
    DB<106> safe_reverse \%h => (1, ["a", "b"], 2, "c")

    That kind of hash (reference) drives me nuts as it makes using the values cumbersome due to the need to test the type before use. (That reminds me to write a utility sub to convert (plain) scalar to one-element array reference.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-10-01 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (41 votes), past polls