Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-25 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found