http://qs321.pair.com?node_id=50164


in reply to What Happened...(perils of porting from c)

i have to make a disclaimer:
i understand what your code is trying to do according to what the english says it is doing, but some of your pointer derefs i'm not certain i follow. Such as the following:
#oops...pointer arithmetic.. #curlevel = curvals + level; $curlevel = $level; # shouldn't this be more like: $curlevel = @$curvals + $level; # or some such? i'm not entirely certain as to why curvals got dr +opped here...
All i did with your code is a more perl-esque rewrite dropping out variables that aren't needed (or aren't used properly) and streamlining here and there. Also, i took out the infinite for loop since it seemed there are better ways to check for what you're doing. This could be not what you're looking for at all, but it seems to be what you have written already, just simplified.

NOTE: there is no error checking for incoming variables in this version.

#!/usr/bin/perl -w package forFun; use strict; use Exporter; @ISA = qw(Exporter); @EXPORT = qw(&ForFun); sub ForFun { my ($startval, $endval, $workfunc) = @_; my ($level, $curvals) = (0, []); push @$curvals, $_ foreach (@$startval); while ($level > -1) { $level = @$startval - 1; while ($curvals->[$level] <= $endval->[$level]) { &$workfunc($curvals); ($curvals->[$level])++; } $level--; while (++($startval->[$level]) > $endval->[$level]); last if (--$level < 0); } } return; } 1;
i tried to follow the pointer logic and your codes' logic as well as possible. i'd definitely suggest going with tilly's code, as it seems much more adaptable and extensible.

Hope this helps,

jynx

ps the code above is untested but should be able to work pretty much as is...