Re: Arbitrarily Many Nested Loops



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} *
);
}
}
}



.



Relevant Pages

  • Re: Arbitrarily Many Nested Loops
    ... decide at run time to have how nested levels to go (probably be on the ... Some redundency left in for clarity and ...
    (comp.lang.perl.misc)
  • Re: Arbitrarily Many Nested Loops
    ... decide at run time to have how nested levels to go (probably be on the ... Some redundency left in for clarity and ... for dynamic code generation. ...
    (comp.lang.perl.misc)
  • Re: subroutine definitions
    ... > application of prototypes to allow inconsistent calling syntax does not seem to ... > me to add any clarity. ... but there-in lies the true power and true nightmare that is Perl. ...
    (perl.beginners)
  • Re: Arbitrarily Many Nested Loops
    ... decide at run time to have how nested levels to go (probably be on the ... You need to work a little on explaining the problem and algorithm. ...
    (comp.lang.perl.misc)
  • Re: [ANN] Groovy Release JSR-04
    ... "Groovy is a JVM hosted scripting language that combines the speed of GW-Basic with the clarity of Perl. ... This release include really major bug fixes, the sort that always make me wonder how anyone was using this ...
    (de.comp.lang.java)