Solved

Simple question about associative arrays in Perl

Posted on 2001-07-01
2
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

630 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