Link to home
Start Free TrialLog in
Avatar of JohnBlazes
JohnBlazes

asked on

Simple question about associative arrays in Perl

I have been learning Perl from a book called "Perl in Examples" and there within is a segment about associative arrays (hashes) that kind of breezes over the how it worx..and basically gives a little example of what to type and what will happen. Here is the code:
%associativeArray = ("Jack A.", "Dec 2", "Joe B.", "June 2", "Jane C.", "Feb 13");
$associativeArray{"Jennifer S."} = "Mar 20";
print "Joe's birthday is: " . $associativeArray{"Joe B."} . "\n";
print "Jennifer's birthday is: " . $associativeArray{"Jennifer S."} . "\n";

This program will print the following:

Joe's birthday is: June 2
Jennifer's birthday is: Mar 20

....all of this seems fine and dandy and I've tried it and it worx fine..and i can see how perl came upon Jennifer's birthday..what i don't understand is how it decided that Joe's birthday would b June 2..does it just associate "Joe B." with "June 2" becuz "Joe B." precedes "June 2"?
..umm i think that makes sense..i would really appreciate any help u have to offer!                                          


Avatar of mnashadka
mnashadka

If you initialize an associative array that way, it will make every odd value a key and every even value the corresponding value.  i.e. Jack A. would correspond with Dec 2, Joe B. with June 2, etc.  It's a very powerful feature, but I think that maybe perl allows you to use the associative array too many ways, but I'm sure that others would disagree.
ASKER CERTIFIED SOLUTION
Avatar of wkmatt42
wkmatt42

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial