I’m having trouble translating a subroutine from Perl to PHP (I’m new to Perl).
The entire subroutine is as follows:
sub find_all_subsets {
if (1 == scalar (@_)) {return [@_]}
else {
my @all_subsets = () ;
my $last_item = pop (@_) ;
my @first_subsets = find_all_subsets (@_) ;
foreach my $subset (@first_subsets) {
push (@all_subsets, $subset) ;
my @ext_subset = @{$subset} ;
push (@ext_subset, $last_item) ;
push (@all_subsets, [@ext_subset]) ;
}
push (@all_subsets, [$last_item]) ;
return (@all_subsets) ;
}
}
My problem is that I really don’t quite understand the Perl syntax, so I’m having trouble writing these @{$subset}, [@ext_subset] and [$last_item] in PHP.
Thanks and sorry if the question is stupid.
When I run
(+ the original script) the output is
Please note the [ ] in the output, we’ll come back to them later.
produces
so far so good. Now back to the [ ]. Data::Dumper chose this [ ] and not ( ) beacuse it’s not an array but an array-reference (bare me if I don’t use the correct terms; perl is really not my strong suit). Let’s change the perl test script and look at the effect all those reference thingies have.
and the output changes to
You see, I change $x2 after the call to find_all_subsets() but still in the result the new value is used and Data::Dumper marks the “value” as a reference ( \99 instead of simply 99 ). Do you need this feature in your php script, too?