Re: Arbitrarily Many Nested Loops
- From: Tim Kazner
- Date: Thu, 30 Mar 2006 16:21:00 -0800
On Thu, 30 Mar 2006 16:14:29 -0800, Tim Kazner wrote:
On 29 Mar 2006 16:51:14 -0800, "Jacob JKW" <jacobcdf@xxxxxxxxx> wrote:[snipped]
This is what I have:
-------------
#!perl
for (my $i = 0; $i<=$n_ra->[0]; $i++) {
for (my $j = 0; $j<=$n_ra->[1]; $j++) {
for (my $k = 0; $k<=$n_ra->[2]; $k++) {
$prob_ra->[$i+$j+$k] += (
$f_raa->[0]->[$i] *
$f_raa->[1]->[$j] *
$f_raa->[2]->[$k] *
);
}
}
-------------
But that's obviously messy and more imprtantly I'd like to be able to
decide at run time to have how nested levels to go (probably be on the
order of 50 or 60). I assume that there's a canonical manner in which
this should be handled (using closures I'd guess) but I can't
sufficiently summarize my issue to make it Google-able.
Any advice?
Many Thanks,
J.
Not sure if this is what you are trying to do
but. Some redundency left in for clarity and
its untested (not debugged).
Forgot the output (if its necessary)
my $isize = 5;
my $jsize = 20;
my $ksize = 60;
my %prob_ra = ();
my %ihash = ();
my %jhash = ();
my %khash = ();
my @f_raa = (\%ihash, \%jhash, \%khash);
# assign i,j,k hash data
for (0..$isize) { $ihash{$_} = $_;}
for (0..$jsize) { $jhash{$_} = $_;}
for (0..$ksize) { $khash{$_} = $_;}
my @n_ra = ($isize, $jsize, $ksize);
or
#@n_ra = (keys %ihash, keys %jhash, keys %khash);
for (my $i = 0; $i<=$n_ra[0]; $i++) {
for (my $j = 0; $j<=$n_ra[1]; $j++) {
for (my $k = 0; $k<=$n_ra[2]; $k++) {
$prob_ra{$i+$j+$k} += (
$f_raa[0]->{$i} *
$f_raa[1]->{$j} *
$f_raa[2]->{$k} *
);
}
}
}
foreach my $key (keys %prob_ra) {
print "$key \t = ".$prob_ra{$key}."\n";
}
.
- References:
- Arbitrarily Many Nested Loops
- From: Jacob JKW
- Re: Arbitrarily Many Nested Loops
- From: Tim Kazner
- Arbitrarily Many Nested Loops
- Prev by Date: Re: Arbitrarily Many Nested Loops
- Next by Date: FAQ 2.7 Is there an ISO or ANSI certified version of Perl?
- Previous by thread: Re: Arbitrarily Many Nested Loops
- Next by thread: FAQ 2.14 Where are the archives for comp.lang.perl.misc?
- Index(es):
Relevant Pages
|
|