Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
> 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)


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


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"])

In reply to Re^2: inverting hash / grouping values by LanX
in thread inverting hash / grouping values by LanX

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [erix]: "surveillance companies" have been rebranded as "social media"
    [LanX]: I'm using whatapp though :-/
    [LanX]: Snowden remins me of Evan Spiegel (... start of a new conspiracy theory)
    [LanX]: yes! The same person!
    [Eily]: LanX can't be, they're both on the picture at the same time. Duh
    [LanX]: They hired a Hollywood producer to fake the pic

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (10)
    As of 2018-03-19 15:30 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (240 votes). Check out past polls.