Solved

Simple question about associative arrays in Perl

Posted on 2001-07-01
2
324 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
ID: 6246536
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
ID: 6246541
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
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…

947 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

21 Experts available now in Live!

Get 1:1 Help Now