user5861285 user5861285 - 1 year ago 93
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 Source

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";

The result as you expect.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download