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: [OT] Javascript "for in"
    ... If you use an object to iterate over, ... never be sure if you're actually iterating over an array. ... close to Perl, funnily enough. ... the fact that objects are merely hashes of properties felt very ...
    (comp.lang.perl.misc)
  • 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: [OT] Javascript "for in"
    ... If you use an object to iterate over, ... never be sure if you're actually iterating over an array. ... The lack of lists is one of the things I find most annoying ... close to Perl, funnily enough. ...
    (comp.lang.perl.misc)
  • 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)