1

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?

1
  • 1
    You can do this recursively. Have you tryed anything so far? Commented Sep 8, 2016 at 8:25

3 Answers 3

2

Here's a slightly more idiomatic solution.

my @arr = qw[mother child1 child2 child3];

say $_ + 1, '. ', join ('/', @arr[0 .. $_]) for 0 .. $#arr;
Sign up to request clarification or add additional context in comments.

Comments

2

Do you need the individual paths, or do you just want to join all the segments?

To do the latter you can just write

my $path = join '/', @arr;

(By the way, that's an awful identifier. The @ tells us that it's an array so the arr adds nothing. I don't know what your data represents, but perhaps @segments would be better.)

But if you need the loop, you can do this

use strict;
use warnings 'all';
use feature 'say';

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

for my $i ( 0 .. $#arr ) {

    my $path = join '/', @arr[0 .. $i];

    say $path;
}

output

mother
mother/child1
mother/child1/child2
mother/child1/child2/child3

Note that this is essentially the same algorithm as Dave Cross shows but I have used a standard block for loop as I imagine that you will want to do something with the paths other than printing them, and I have removed the numbering as I think that was just an illustrative part of your question.

Comments

0

You can try with this solution:

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

The result as you expect.

output

.mother
1.mother/child1
2.mother/child1/child2
3.mother/child1/child2/child3



Update

That should have been

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

output

1.mother
2.mother/child1
3.mother/child1/child2
4.mother/child1/child2/child3

4 Comments

I doubt if the OP requires the numbers printing before each path. In any case, $content .= '/' if ($i>1) is better written as $content .= '/' if $content, so the $i variable is unnecessary.
What you've written is very compact. Perl will ignore whitespace between tokens, so you should make the most of that to lay out your program so that it is readable. Also, please test your code before you post it. Look at the output from your (revised) code. If you had use strict and use warnings 'all' in place as you should have then you would have seen the warning message Use of uninitialized value $i in concatenation (.) or string
Please don't edit your posts so the earlier contributions make no sense.
@Borodin: I corrected it, I'm new to stackoverflow and very appreciate your prompt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.