
Laurent_R
Veteran
/ Moderator
Mar 18, 2013, 3:51 PM
Post #4 of 4
(748 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:
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:
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:
print "$year2013{feb}[0] \n"; which will output:
Pope Benedict resigns from duty. Or, if I want to print all the news of March 2013:
print $_, "\n" foreach @{$year2013{mar}}; which will print out:
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:
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.
|