Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Creating a Dynamic Nested Loops for HoA

by injunjoel (Priest)
on Dec 06, 2005 at 23:29 UTC ( #514668=note: print w/replies, xml ) Need Help??


in reply to Creating a Dynamic Nested Loops for HoA

Greetings,
Nothing novel but this question looked like fun so here are my 2 cents. I will leave the Benchmark tests to those more versed in such things :}
#!/usr/bin/perl -w use strict; use Data::Dumper; # The HoA below may come in varying # sizes: from "key1" to "key 50" my $hash = { 'key1' => [ 1, 2, 3, 4 ], 'key2' => [ 10, 20, 30 ], 'key3' => [ 100, 200, 300 ], 'key4' => [ 'a','b','c','dood'] }; my (@b, @base); #compute combos for(sort keys %{$hash}){ concat_arrays(\@b, $hash->{$_}); } #filter computations @base = grep{ my $count = () = $_ =~ m/\-/g; ($count == (scalar(keys %{$hash})-1)); }sort @base; #double check print $_."\n" for(@base); sub concat_arrays { my ($baseref, $newref) = @_; foreach my $new (@{$newref}){ if(scalar(@{$baseref})){ foreach my $base (@{$baseref}){ push @base, $base." - ".$new; } }else{ push @base, $new; } } @b = @base; }
The output
1 - 10 - 100 - a 1 - 10 - 100 - b 1 - 10 - 100 - c 1 - 10 - 100 - dood 1 - 10 - 200 - a 1 - 10 - 200 - b 1 - 10 - 200 - c 1 - 10 - 200 - dood 1 - 10 - 300 - a 1 - 10 - 300 - b 1 - 10 - 300 - c 1 - 10 - 300 - dood 1 - 20 - 100 - a 1 - 20 - 100 - b 1 - 20 - 100 - c 1 - 20 - 100 - dood 1 - 20 - 200 - a 1 - 20 - 200 - b 1 - 20 - 200 - c 1 - 20 - 200 - dood 1 - 20 - 300 - a 1 - 20 - 300 - b 1 - 20 - 300 - c 1 - 20 - 300 - dood 1 - 30 - 100 - a 1 - 30 - 100 - b 1 - 30 - 100 - c 1 - 30 - 100 - dood 1 - 30 - 200 - a 1 - 30 - 200 - b 1 - 30 - 200 - c 1 - 30 - 200 - dood 1 - 30 - 300 - a 1 - 30 - 300 - b 1 - 30 - 300 - c 1 - 30 - 300 - dood 2 - 10 - 100 - a 2 - 10 - 100 - b 2 - 10 - 100 - c 2 - 10 - 100 - dood 2 - 10 - 200 - a 2 - 10 - 200 - b 2 - 10 - 200 - c 2 - 10 - 200 - dood 2 - 10 - 300 - a 2 - 10 - 300 - b 2 - 10 - 300 - c 2 - 10 - 300 - dood 2 - 20 - 100 - a 2 - 20 - 100 - b 2 - 20 - 100 - c 2 - 20 - 100 - dood 2 - 20 - 200 - a 2 - 20 - 200 - b 2 - 20 - 200 - c 2 - 20 - 200 - dood 2 - 20 - 300 - a 2 - 20 - 300 - b 2 - 20 - 300 - c 2 - 20 - 300 - dood 2 - 30 - 100 - a 2 - 30 - 100 - b 2 - 30 - 100 - c 2 - 30 - 100 - dood 2 - 30 - 200 - a 2 - 30 - 200 - b 2 - 30 - 200 - c 2 - 30 - 200 - dood 2 - 30 - 300 - a 2 - 30 - 300 - b 2 - 30 - 300 - c 2 - 30 - 300 - dood 3 - 10 - 100 - a 3 - 10 - 100 - b 3 - 10 - 100 - c 3 - 10 - 100 - dood 3 - 10 - 200 - a 3 - 10 - 200 - b 3 - 10 - 200 - c 3 - 10 - 200 - dood 3 - 10 - 300 - a 3 - 10 - 300 - b 3 - 10 - 300 - c 3 - 10 - 300 - dood 3 - 20 - 100 - a 3 - 20 - 100 - b 3 - 20 - 100 - c 3 - 20 - 100 - dood 3 - 20 - 200 - a 3 - 20 - 200 - b 3 - 20 - 200 - c 3 - 20 - 200 - dood 3 - 20 - 300 - a 3 - 20 - 300 - b 3 - 20 - 300 - c 3 - 20 - 300 - dood 3 - 30 - 100 - a 3 - 30 - 100 - b 3 - 30 - 100 - c 3 - 30 - 100 - dood 3 - 30 - 200 - a 3 - 30 - 200 - b 3 - 30 - 200 - c 3 - 30 - 200 - dood 3 - 30 - 300 - a 3 - 30 - 300 - b 3 - 30 - 300 - c 3 - 30 - 300 - dood 4 - 10 - 100 - a 4 - 10 - 100 - b 4 - 10 - 100 - c 4 - 10 - 100 - dood 4 - 10 - 200 - a 4 - 10 - 200 - b 4 - 10 - 200 - c 4 - 10 - 200 - dood 4 - 10 - 300 - a 4 - 10 - 300 - b 4 - 10 - 300 - c 4 - 10 - 300 - dood 4 - 20 - 100 - a 4 - 20 - 100 - b 4 - 20 - 100 - c 4 - 20 - 100 - dood 4 - 20 - 200 - a 4 - 20 - 200 - b 4 - 20 - 200 - c 4 - 20 - 200 - dood 4 - 20 - 300 - a 4 - 20 - 300 - b 4 - 20 - 300 - c 4 - 20 - 300 - dood 4 - 30 - 100 - a 4 - 30 - 100 - b 4 - 30 - 100 - c 4 - 30 - 100 - dood 4 - 30 - 200 - a 4 - 30 - 200 - b 4 - 30 - 200 - c 4 - 30 - 200 - dood 4 - 30 - 300 - a 4 - 30 - 300 - b 4 - 30 - 300 - c 4 - 30 - 300 - dood


-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2019-07-20 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?