definitely. What you want is a hash. It would look like this:
 
###
 
my %animalandfood = (
    'cat' => 'meow mix',
    'dog' => 'dog chow',
    'ferret' => 'ferret food',
    'mouse' => 'cheese'  ### changed this one on ya!
    );
   
foreach my $animal (keys %animalandfood)
{
    print "animal = $animal and food = $animalandfood{$animal}\n";
}
 
###
 
Note that hashes do not necessarily come out in the order you put them in (I've never quite understood why), so it can be a little unpredictable and you would need to sort them some way if you wanted them in a particular order. Also, you can dynamically grow a hash too, like so:
 
$animalandfood{'manatee'} = 'manatee chow';
 
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Friday, June 27, 2003 9:28 AM
To: [EMAIL PROTECTED]
Subject: [Perl-unix-users] Nesting foreach using multiple arrays


Hi, new to the list here; quick question !

How do I nest multiple arrays in one 'foreach' statement ?

ex.

@animals = ("cat",
"dog",
"ferret",
"mouse"
);

@petfood = ("meow mix",
"dog chow",
"ferret food",
"mouse food"
);

foreach $object (@animals) {
  print "animal = $object and food = @petfood";
  }

The results I am lookg for are

animal = cat and food = meow mix
animal = dog and food = dog chow
animal = ferret and food = ferret food
animal = mouse and food = mouse food

Thanks alot for you help

Is there a better way of doing this also ?

**DAN**

Reply via email to