Gossamer Forum
Home : General : Perl Programming :

Analyzing an ARRAY within another ARRAY...

Quote Reply
Analyzing an ARRAY within another ARRAY...
I generated an array called @category. And in @category, I've stored an items array, @items, which contains several item. This was accomplished by using
push (@category, @items);
-----------------------------

Now, I'd like to do a loop that goes through each @items array and manipulates the individual items within each @items array. I'm using the following code:

for (@category) {
@items = $_;
item1 = $item[1] + whatever....;
item2 = $item[2] + whatever...;
item3 = $item[3]; # and so on....

} # end for loop

But for some reason I don't get what I expect (i.e.- item3 does not correspond to item[3]). It especially goes crazy if one of the items within the @items array is empty.

PLEASE, SOMEONE HELP ME WITHIN THIS. I'M GETTING BALD PULLING MY HAIR OUT OF FRUSTRATION.

[This message has been edited by fordona (edited March 20, 1999).]

[This message has been edited by fordona (edited March 20, 1999).]
Quote Reply
Re: Analyzing an ARRAY within another ARRAY... In reply to
This:

push (@category, @items);

is not doing what you think it is. What it is doing is putting all the elements of @items onto the end of @category. So if you had:

@category = (1,2,3);
@items = (8,9,10);

then push(@category, @items) would result in:

@category = (1,2,3,8,9,10);

It's all one big array. To have an array of array's you'll need to use references. See:

http://language.perl.com/.../pdsc-1-verbose.html

Cheers,

Alex