?
Solved

Simple question about associative arrays in Perl

Posted on 2001-07-01
2
Medium Priority
?
334 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

718 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