Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I share the concerns of others about mixing too much Perl majick together: the result may blow up in your (or your maintainer's) face in very unexpected ways. But as to some tangential questions...

Is there an efficient way to check whether the values of a hash are unique?
c:\@Work\Perl\monks>perl -wMstrict -le "my %h = qw(a 1 b 2 c 3 d 1); ;; my %r = reverse %h; keys %r == keys %h or die qq{hash @{[ %h ]} not unique}; " hash c 3 a 1 b 2 d 1 not unique at -e line 1.
... a solution to return several values if the hash is not unique ...
The general approach to maintaining several values for a hash key is an anonymous array. Maybe something like:
c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; my %h = ( a => [ 1 ], b => [ 2, 99, 88 ], c => [ 3 ], d => [ 1, 99 ] ); ;; my %r; for my $hk (keys %h) { push @{ $r{$_} }, $hk for @{ $h{$hk} }; } dd \%r; " { 1 => ["a", "d"], 2 => ["b"], 3 => ["c"], 88 => ["b"], 99 => ["b", "d +"] }
Update: Note that this inversion approach for multi-value hashes is not round-tripable unless you consider the arrays of values to be unordered lists:
c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; use Data::Dump qw(dd); ;; my %h = ( a => [ 1 ], b => [ 2, 99, 88 ], c => [ 3 ], d => [ 1, 99 ] ); ;; my %r = invert(%h); dd \%r; ;; my %rr = invert(%r); dd \%rr; ;; is_deeply \%rr, \%h, 'round trip'; ;; done_testing; ;; exit; ;; sub invert { my %h = @_; my %r; for my $hk (keys %h) { push @{ $r{$_} }, $hk for @{ $h{$hk} }; } return %r; } " { 1 => ["a", "d"], 2 => ["b"], 3 => ["c"], 88 => ["b"], 99 => ["b", "d +"] } { a => [1], b => [99, 88, 2], c => [3], d => [99, 1] } not ok 1 - round trip # Failed test 'round trip' # at -e line 1. # Structures begin differing at: # $got->{b}[0] = '99' # $expected->{b}[0] = '2' 1..1 ok 2 - no warnings 1..2 # Looks like you failed 1 test of 2.


Give a man a fish:  <%-{-{-{-<


In reply to Re: Dualvar via table by AnomalousMonk
in thread Dualvar via table by Dirk80

Title:
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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2020-03-31 22:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      To "Disagree to disagree" means to:









      Results (186 votes). Check out past polls.

      Notices?