我有一个带有x个项目的数组:
my @arr= qw( mother child1 child2 child3);
现在我想模仿这个数组。每个循环都应附加一个条目:
我如何用Perl来实现这一点?
您可以尝试使用以下解决方案:
my @arr= qw( mother child1 child2 child );
my $content;
my $i;
foreach (@arr){
$content .= '/' if ($content);
$content .= $_;
print "$i.$content\n";
$i++;
}
结果如您所愿。
.mother
1.mother/child1
2.mother/child1/child2
3.mother/child1/child2/child3
那应该是
use strict;
use warnings 'all';
my @arr= qw( mother child1 child2 child3 );
my $content;
my $i = 1;
foreach ( @arr ) {
$content .= '/' if $content;
$content .= $_;
print "$i.$content\n";
++$i;
}
1.mother
2.mother/child1
3.mother/child1/child2
4.mother/child1/child2/child3
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句