LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PERL: Size of an array of an Array (http://www.linuxquestions.org/questions/programming-9/perl-size-of-an-array-of-an-array-155845/)

inspleak 03-10-2004 09:40 AM

PERL: Size of an array of an Array
 
Hey guys... here is the situation:
@list1 = ("one", "two", "three");
@list2 = ("myList2");
@fooList = ([@list2], [@list1]);

Basically what I want to do is walk down each array in
@fooList. For instance I want to read data from @list2,
element at a time, then @list1, but I want to be able to
add new arrays to @fooList at any time. So I don't know
the size of @fooList or any of the arrays ahead of time.

So I want it to work like this (though it doesn't :))

foreach $thing1 (@fooList)
{
foreach $currentThing ($fooList[$thing1]);
{
print "$currentThing\n";
}
}

So what I would like to happen in this case is have it print out each
element in each list.

so output would be like:
mylist2
one
two
three

I could use "for" loops but I would need to know the sizes of the arrays inside @foolist on the fly, and I couldn't figure out how to do that either..

Or I might just be crazy..

Thanks much for the help!

david_ross 03-10-2004 02:22 PM

Try this:
Code:

#!/usr/bin/perl

@list1 = ("one", "two", "three");
@list2 = ("myList2");
@fooList = ([@list2], [@list1]);


foreach $thing (@fooList){
 foreach $currentThing (@{$thing}){
  print "$currentThing\n";
 }
}

exit;


inspleak 03-10-2004 02:24 PM

Man.. thats great! thanks so much..

I didn't realize you could reference like that..

very cool...

thanks!


All times are GMT -5. The time now is 12:53 PM.