confused about nesting arrays

qxcozjn02_at_sneakemail.com
Date: 05/29/04


Date: 29 May 2004 01:33:32 -0700

Hello, I have run into a problem while writing a perl script. I am
fairly new to perl, and have done my best to search google and read
the perl documentation (as well as my o'reilly books) to try and solve
this problem I am having.

Basically, I need to nest an array within an array within a hash
element, then I need to use foreach and iterate through the first
array and then another foreach to iterate through the second array.

For some reason this isn't working for me. Here is a simplified
example of the code I am writing,

@fruits = ('apple', 'cherry', 'orange', 'peach');
@vegetables = ('carrot', 'asparagus', 'onion');

%world->{food}->[0] = (\@fruits, \@vegetables);

foreach $a (%world->{food}) {
   foreach $b ($a) {
      print $b[0];
   }
}

I'm sure i'm doing something wrong, but i'm not sure how i'm supposed
to do this. Can anyone explain what is wrong with this code, and give
me an example of the correct way to go about this? That should point
me in the right direction hopefully heh.

Thanks,
Matthew.



Relevant Pages

  • Re: Stop thinking in C
    ... > know that there are some idioms in other languages which aren't uses ... In languages like Ruby or Perl you have other methods to ... My idea was, that, to just iterate over an array, in Lisp you have may ...
    (comp.lang.lisp)
  • Re: getting end of an array
    ... like you should be writing a bash script file (Perl isn't ... Perl array '@ip' of output records. ... You're writing a Perl program which essentially runs 'ps' ...
    (perl.beginners)
  • Re: @array =to=> $array
    ... Speaking of nitpicks: ... > If you DO need to iterate across all the indices for an array (rarely ... This is not Bad Perl. ...
    (perl.beginners)
  • Re: pass 2d array to C function
    ... > the C function that I am invoking from perl? ... Are you writing the C function, ... what kind of 2D array does the C function ... Xho ...
    (comp.lang.perl.misc)
  • Re: extract a column from 2 dimensional array
    ... ravi wrote in comp.lang.perl.misc: ... > there an efficient way to do this or we have to only iterate each ... Unless your array is stored by columns (the specification "2-D array" ... Perl 6 will have multidimensional slices, ...
    (comp.lang.perl.misc)