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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.