Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Massive expansion of a hash of arrays?

by djerius (Beadle)
on Jul 18, 2014 at 21:10 UTC ( [id://1094267]=note: print w/replies, xml ) Need Help??


in reply to Massive expansion of a hash of arrays?

Update: Whoops! Looks like Christoforo suggested this first. Not sure why that post's code is all struck out though.

Set::CrossProduct to the rescue:

use strict; use warnings; use Set::CrossProduct; my %hash=('ID' => { 'key1' => [ qw/ key1_val1 key1_val2/ ], 'key2' => [ qw/ key2_val1 key2_val2/ ] } ); foreach my $id ( keys %hash ) { my $keys = $hash{$id}; my $set = Set::CrossProduct->new( [ values %$keys ] ); print join( ', ', $id, @$_ ), "\n" while $_ = $set->get; }
Results in
ID, key2_val1, key1_val1 ID, key2_val1, key1_val2 ID, key2_val2, key1_val1 ID, key2_val2, key1_val2
Not exactly the order of your requested output, but that's because your keys are in a hash.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found