user5861285 user5861285 - 2 months ago 6
Perl Question

Join array items in loop with perl

I have an array with x items:

my @arr= qw( mother child1 child2 child3);


Now i want to ietrate this array. Every Loop should append an entry:


  1. mother

  2. mother/child1

  3. mother/child1/child2

  4. mother/child1/child2/child3



How i can realize this with perl?

Answer

You can try with this solution:

my @arr= qw( mother child1 child2 child3);
my $i=1;
my $content;
foreach (@arr){
  $content .= '/' if ($i>1);
  $content .= $_;
  print "$i.$content\n";
  $i++;
}

The result as you expect.

Comments