Re: Looping through an anonymous array of arrays
[email protected] (Rob Dixon)
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
Rodrick Brown wrote:
>
> my $arrayRef = [ 1, 2, 3, ['a', 'b', 'c', ["Hello"] ]];
>
> I have no problem returning single elements but how would one walk this list
> of elements with say a for loop?
use strict;
use warnings;
my $arrayRef = [ 1, 2, 3, ['a', 'b', 'c', ["Hello"] ]];
my @flat = $arrayRef;
for (my $i = 0; $i < @flat; $i++) {
splice @flat, $i, 1, @{$flat[$i]} while ref $flat[$i] eq 'ARRAY';
}
print "$_\n" foreach @flat;
Rob