On Mon, 2002-05-06 at 12:04, Jason Frisvold wrote:
> Here's another simple question....  I have an array of arrays and I want
> to use a foreach to do something with each entry...  currently I do this
> :
> 
> foreach my $item (@myarray) {
>       my ($item1, $item2) = @$item;
>       <do stuff here>
> }
> 
> Is there a way to combine the first 2 lines?  Is it any faster and less
> memory intensive?  (granted it's not much now, but hey, I like
> optimizing things to death...)
<snip />

Since you seem interested in having named variables instead of indexes
into an array you may be better served by an array of hashes.  The code
would look like this:

my @myarray = (
        {
                count => 10,
                type  => 'apple'
        },
        {
                count => 5,
                type  => 'oranges'
        }
);

foreach my $item (@myarray) {
        print "The are $item->{'count'} $item->{'type'}(s)\n";
}
 
-- 
Today is Sweetmorn the 53rd day of Discord in the YOLD 3168
Umlaut Zebra über alles!

Missile Address: 33:48:3.521N  84:23:34.786W


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to