Dear fellow monks,
I have a code below that takes a HoA as input, then processing the combination of the array elements as shown
in the result below.
My problem is that the hash will come in varying sizes
from time to time. My code below although already gave
a correct results, it is awfully
rigid. How can I make it more flexible?
Is there any modules that can come to help to create this
kind of dynamic nested loops?
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 ],
};
# My code below is "hard-coded"
# How can I change it to accomodate
# dinamycally changing hash size above?
my @val1 = @{$hash->{'key1'}};
my @val2 = @{$hash->{'key2'}};
my @val3 = @{$hash->{'key3'}};
foreach my $val1 (@val1) {
foreach my $val2 (@val2) {
foreach my $val3 (@val3) {
print "$val1 - $val2 - $val3\n";
}
}
}
Note that the example of input sample ($hash) is simplified.
In my production code it actually comes in form
of HoAoA.
Here is the results of my code above:
1 - 10 - 100
1 - 10 - 200
1 - 10 - 300
1 - 20 - 100
1 - 20 - 200
1 - 20 - 300
1 - 30 - 100
1 - 30 - 200
1 - 30 - 300
2 - 10 - 100
2 - 10 - 200
2 - 10 - 300
2 - 20 - 100
2 - 20 - 200
2 - 20 - 300
2 - 30 - 100
2 - 30 - 200
2 - 30 - 300
3 - 10 - 100
3 - 10 - 200
3 - 10 - 300
3 - 20 - 100
3 - 20 - 200
3 - 20 - 300
3 - 30 - 100
3 - 30 - 200
3 - 30 - 300
4 - 10 - 100
4 - 10 - 200
4 - 10 - 300
4 - 20 - 100
4 - 20 - 200
4 - 20 - 300
4 - 30 - 100
4 - 30 - 200
4 - 30 - 300
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.