J
JR
I'm having trouble understanding the logic flow of recipe 4.3 in the
Perl Cookbook (the below script is only slightly different from the
recipe in the Cookbook), and am hoping someone can clarify it for me.
Here's where I'm confused:
In the below permute subroutine, I don't understand how the $i
variable can ever reach 1 and 2. It is initially set to 0 before the
for loop is entered. Immediately after the unshift line, the permute
subroutine is recursively and unconditionally called. When the else
loop is hit on any further calls, the $i variable is again set to 0,
yet the below output clearly indicates that the variable is, at some
point, being incremented. I don't see how this is possible, yet,
clearly it is. I notice that the variable is always 1 or 2
immediately after a given group of perms is printed, but this just
isn't enough of a clue to help me understand how this works. I must
be missing the obvious here. Can someone please point it out to me?
Thanks much.
use strict;
use warnings;
my @array = ('A'..'C');
permute([@array]);
sub permute {
my @items = @{ $_[0] };
my @perms = @{ $_[1] } if defined @{ $_[1] };
if (!@items) {
print "\@perms=@perms\n";
}
else {
my (@newitems, @newperms);
my $i = 0;
for my $i (0..$#items) {
print "\$i=$i\n";
@newitems = @items;
@newperms = @perms;
## Splice @newitems element and prepend it to @newperms
unshift(@newperms, splice(@newitems, $i, 1));
permute([@newitems], [@newperms]);
}
}
}
__END__
$i=0
$i=0
$i=0
@perms=C B A
$i=1
$i=0
@perms=B C A
$i=1
$i=0
$i=0
@perms=C A B
$i=1
$i=0
@perms=A C B
$i=2
$i=0
$i=0
@perms=B A C
$i=1
$i=0
@perms=A B C
Perl Cookbook (the below script is only slightly different from the
recipe in the Cookbook), and am hoping someone can clarify it for me.
Here's where I'm confused:
In the below permute subroutine, I don't understand how the $i
variable can ever reach 1 and 2. It is initially set to 0 before the
for loop is entered. Immediately after the unshift line, the permute
subroutine is recursively and unconditionally called. When the else
loop is hit on any further calls, the $i variable is again set to 0,
yet the below output clearly indicates that the variable is, at some
point, being incremented. I don't see how this is possible, yet,
clearly it is. I notice that the variable is always 1 or 2
immediately after a given group of perms is printed, but this just
isn't enough of a clue to help me understand how this works. I must
be missing the obvious here. Can someone please point it out to me?
Thanks much.
use strict;
use warnings;
my @array = ('A'..'C');
permute([@array]);
sub permute {
my @items = @{ $_[0] };
my @perms = @{ $_[1] } if defined @{ $_[1] };
if (!@items) {
print "\@perms=@perms\n";
}
else {
my (@newitems, @newperms);
my $i = 0;
for my $i (0..$#items) {
print "\$i=$i\n";
@newitems = @items;
@newperms = @perms;
## Splice @newitems element and prepend it to @newperms
unshift(@newperms, splice(@newitems, $i, 1));
permute([@newitems], [@newperms]);
}
}
}
__END__
$i=0
$i=0
$i=0
@perms=C B A
$i=1
$i=0
@perms=B C A
$i=1
$i=0
$i=0
@perms=C A B
$i=1
$i=0
@perms=A C B
$i=2
$i=0
$i=0
@perms=B A C
$i=1
$i=0
@perms=A B C