note
broquaint
For those who don't grok pseudocode here's a perl translation for directory traversal without recursion
<code>
my @stack = ".";
while(my $cd = pop @stack) {
opendir(my $dh, $cd) or die("bad dir - $!");
foreach (readdir($dh)) {
next if /^\./;
print $_.$/;
push @stack, "$cd/$_" if -d "$cd/$_";
}
}
</code>
and with recursion
<code>
&traverse(".");
sub traverse {
my $dir = shift;
opendir(my $dh, $dir) or die("bad dir - $!");
foreach (readdir($dh)) {
next if /^\./;
print $_.$/;
&traverse("$dir/$_") if -d "$dir/$_";
}
}
</code>
That's a pretty neat 'trick' which I guess might be how optimising compilers implement some forms of recursion (or something).
<br>
HTH
<p>
broquaint
151492
151693