Solved

Simple question about associative arrays in Perl

Posted on 2001-07-01
2
322 Views
Last Modified: 2008-03-10
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!                                          


0
Comment
Question by:JohnBlazes
2 Comments
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
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.
0
 

Accepted Solution

by:
wkmatt42 earned 50 total points
Comment Utility
A better way to write the same thing is:

%associativeArray =
("Jack A." => "Dec 2",
 "Joe B." => "June 2",
 "Jane C." => "Feb 13");

You were correct in your assumption about how the items were reletated: 2 is associated with 1, 4 with 3, etc. The notation above makes that a little more obvious, so I'm surprised your book didn't opt for it.

You'll probably see the term "hash" used more often than "associate array" to refer to the same thing.

Since you're new to Perl, I'd recommend picking up "Programming Perl" by Larry Wall, etc. There's a companion "Learning Perl" which is nice too, but your bookshelf won't be complete without "Programming Perl".


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now