Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Step through Array of Arrays with unknown Size

Status
Not open for further replies.

inspleak

Technical User
Mar 10, 2004
14
US
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.

(essentially I want to make more arrays to add to @fooList and not have to change any of my code later on in the script. So I would just make a new Array and add that Array to the Array of Arrays -@fooList)

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 I guess 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!
 
No need to know the sizes of the array. You can just use a foreach loop like this:
Code:
@list1 = ("one", "two", "three");
@list2 = ("myList2");
@fooList = ([@list2], [@list1]);

foreach $refarray (@fooList) {
    foreach $item (@{$refarray}) {
        print "$item\n";
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top