我无法将子程序从Perl转换为PHP(我是Perl的新手)。 整个子程序如下:
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) ;
}
}
我的问题是我真的不太了解Perl语法,所以我在PHP中编写这些@{$subset}
,[@ext_subset]
和[$last_item]
时遇到了麻烦。
如果问题很愚蠢,谢谢,对不起。
答案 0 :(得分:4)
[]
是一个数组引用运算符,用于将数组转换为数组引用
@{}
是一个数组解引用运算符,将数组引用转换为数组
@a = (1,2,3);
$a = [ @a ];
@b = @{$a}; # now @b ==> (1,2,3)
答案 1 :(得分:2)
当我跑步时
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
my @x = (1,2,3,4);
my @y = find_all_subsets(@x);
foreach my $subset (@y) {
print Dumper($subset), "\n";
}
(+原始脚本)输出
[1]
[1,4]
[1,3]
[1,3,4]
[1,2]
[1,2,4]
[1,2,3]
[1,2,3,4]
[2]
[2,4]
[2,3]
[2,3,4]
[3]
[3,4]
[4]
请注意输出中的[],我们稍后会再回来。
function find_all_subsets (array $x) {
if ( 1>= count($x) ) { // the >= differs from the original script, use == or === if you want to keep it "more original"
return array($x);
}
else {
$all_subsets = array();
$last_item = array_pop($x);
$first_subsets = find_all_subsets($x) ;
foreach ($first_subsets as $subset) {
array_push($all_subsets, $subset);
array_push($subset, $last_item);
array_push($all_subsets, $subset);
}
array_push ($all_subsets, array($last_item));
return $all_subsets;
}
}
$x = array(1,2,3,4);
$y = find_all_subsets($x);
foreach($y as $subset) {
echo '(', join(',', $subset), ")\n";
}
产生
(1)
(1,4)
(1,3)
(1,3,4)
(1,2)
(1,2,4)
(1,2,3)
(1,2,3,4)
(2)
(2,4)
(2,3)
(2,3,4)
(3)
(3,4)
(4)
到目前为止一切顺利。现在回到[]。 Data :: Dumper选择了这个[]而不是()因为它不是一个数组而是一个数组引用(如果我没有使用正确的术语,那就是我; perl真的不是我的强项)。让我们改变perl测试脚本并查看所有这些参考资料的效果。
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
$x2 = 2;
my @x = (1,\$x2,3,4);
my @y = find_all_subsets(@x);
$x2 = 99;
foreach my $subset (@y) {
print Dumper($subset), "\n";
}
,输出变为
[1]
[1,4]
[1,3]
[1,3,4]
[1,\99]
[1,\99,4]
[1,\99,3]
[1,\99,3,4]
[\99]
[\99,4]
[\99,3]
[\99,3,4]
[3]
[3,4]
[4]
你看,我在调用find_all_subsets()之后更改了$ x2 ,但仍然在结果中使用了新值,Data :: Dumper标记了“值” “作为参考(\ 99而不是简单的99)。你的php脚本中也需要这个功能吗?
答案 2 :(得分:1)
这样的事情:
@{$subset} # Return array reference as an ordinary array, not applicable in PHP.
[@ext_subset] # Turn an array into an array reference, not applicable in PHP.
[$last_item] # Create an array with $last_item as the only element, equal to array($last_item) in PHP.
答案 3 :(得分:0)
这是正确的(在php中)吗?
function find_all_subsets($array) {
if (count($array) == 1) {
return $array;
}
else {
$all_subsets = array();
$last_item = array_pop($array);
$first_subsets = find_all_subsets($array);
foreach ($first_subsets as $key => $val) {
array_push($all_subsets, $val);
$ext_subset[] = $val;
array_push($ext_subset, $last_item);
array_push($all_subsets, $ext_subset);
}
array_push($all_subsets, $last_item);
return $all_subsets;
}
}
答案 4 :(得分:0)
如果你只是需要找到所有子集,它就像这样
function subsets($ary) {
$out = array();
$len = count($ary);
for($i = 0; $i < (1 << $len); $i++) {
for($p = array(), $j = 0; $j < $len; $j++)
if($i & (1 << $j))
$p[] = $ary[$j];
$out[] = $p;
}
return $out;
}