CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Elements in Array

 



cluna
Novice

Mar 18, 2013, 9:51 AM

Post #1 of 4 (468 views)
Elements in Array Can't Post

This is a general question and not related to any data that I have. In a hash of arrays, the value can be represented by an array, which is made up of elements. If the elements are variables (meaning they are different for each key), then how can you define each element in the array that makes up the value to use in a program/operation for each key?

Thank you.


Rahul6990
Novice

Mar 18, 2013, 11:48 AM

Post #2 of 4 (460 views)
Re: [cluna] Elements in Array [In reply to] Can't Post

Refer to the below link:

http://www.thegeekstuff.com/2011/09/perl-complex-data-structures/


BillKSmith
Veteran

Mar 18, 2013, 1:54 PM

Post #3 of 4 (451 views)
Re: [cluna] Elements in Array [In reply to] Can't Post

Perl comes with extensive documentation and a utility (perldoc) to access it. At your command line, type perldoc perldoc to learn to use the utility. Three pages are relevant to your question. Use the perldoc utility to access perldata, perlref, and perldsc.

You use several terms incorrectly in your question. Definitions are important.

A hash is a data structure that associates single scalar value with a string called a key. There is no limit to how many key/value pairs are stored in a single hash.

In your case (hash of arrays), the values are all references to arrays. An array is the physical storage which holds a list of scalar elements.

List and element largly conform to our everyday usage. A reference is a perl scalar, similiar to the C pointer. It provides perl with the address and certain other information about the structure that it refers to.

You should now be able to phrase your question clearly, and answer it yourself!
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Mar 18, 2013, 3:51 PM

Post #4 of 4 (445 views)
Re: [BillKSmith] Elements in Array [In reply to] Can't Post

Just a somewhat silly example of HoA. Suppose you want to keep track of some important news of year 2013, and store them month by month.

You could create a hash for year 2013 with a key for each month. And, in each month, an array of all the significant news within this month.

You could do something like this:


Code
my %year2013; # declaring the hash 
$year2013{jan}[0] = "Fireworks on Time Square to celebrate the new year on Jan 1, 2013.";
$year2013{jan}[1] = "Something else that happened in Jan. 2013";
$year2013{feb}[0] = "Pope Benedict resigns from duty.";
$year2013{mar}[0] = "Severe snowstoms in the area of the city of xxx.";
push @{$year2013{mar}}, "Death of Venezuelian President Hugo Chavez";
push @{$year2013{mar}}, "New Argentinian Pope elected in Rome";


I now have the following data structure:
- The %year2013 has three keys: jan, feb and mar.
- The values associated with these keys are references (memory addresses) to anonymous arrays (i.e. arrays that don't have a name).
- the array whose reference is stored in $year{mar} has three elements: the snowstorm, Chavez and Pope news.

If I run the above code lines under the debugger and then look at the content of my hash, I obtain something like this:


Code
  DB<7> x %year2013 
0 'feb'
1 ARRAY(0x20381630)
0 'Pope Benedict resigns from duty.'
2 'jan'
3 ARRAY(0x20080860)
0 'Fireworks on Time Square to celebrate the new year on Jan 1, 2013.'
1 'Something else that happened in Jan. 2013'
4 'mar'
5 ARRAY(0x20381468)
0 'Severe snowstoms in the area of the city of xxx.'
1 'Death of Venezuelian President Hugo Chavez'
2 'New Argentinian Pope elected in Rome'


This says that the %year2013 hash has a 'feb' key and that the corresponding value is an reference to an array (memory address 0x20381630). This array has only one element: the resignation of the Pope. Similarly, the hash has a 'jan' key whose value is an array (located at 0x20080860), with two elements: the fireworks on NY and the 'something else" news. Finally, the 'mar' key of the hash refers to another array, with three elements (snow storm, Chavez's death and new Pope election).

Now, if I want to print the only news in February, I can type:


Code
print "$year2013{feb}[0] \n";

which will output:


Code
Pope Benedict resigns from duty.


Or, if I want to print all the news of March 2013:


Code
print $_, "\n" foreach  @{$year2013{mar}};


which will print out:


Code
Severe snowstoms in the area of the city of xxx. 
Death of Venezuelian President Hugo Chavez
New Argentinian Pope elected in Rome


There are other (and sometimes more concise) ways of doing things (that the spirit of Perl: "TIMTOWTDI") and constructing, populating or accessing the data, but at least, I hope that you get a sense of what it means to create a data structure such as an HoA.

But then, of course, you could contruct more complicated structures. For instance, you may have a super array with one element per year, and each such element containing a reference to a hash such as the one we have just built.

I could just say that the new @events element for 2013 is a reference to the hash I have created before. For example:


Code
my $events[2013] = \%year2013;


I now have an array of hashes of arrays (AoHoA), with so far just one element (2013) of that array populated.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives