Re: Arbitrarily Many Nested Loops
- From: Tim Kazner
- Date: Thu, 30 Mar 2006 16:14:29 -0800
On 29 Mar 2006 16:51:14 -0800, "Jacob JKW" <jacobcdf@xxxxxxxxx> wrote:
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).
my $isize = 5;
my $jsize = 20;
my $ksize = 60;
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} *
);
}
}
}
.
- Follow-Ups:
- Re: Arbitrarily Many Nested Loops
- From: Tim Kazner
- Re: Arbitrarily Many Nested Loops
- References:
- Arbitrarily Many Nested Loops
- From: Jacob JKW
- Arbitrarily Many Nested Loops
- Prev by Date: *flush* (was: Re: Find duplicates in a dat file)
- Next by Date: Re: Arbitrarily Many Nested Loops
- Previous by thread: Re: Arbitrarily Many Nested Loops
- Next by thread: Re: Arbitrarily Many Nested Loops
- Index(es):
Relevant Pages
|