Click to See Complete Forum and Search --> : Adding elements to this data structure


eXoR
08-27-2004, 07:50 AM
I've found the following data structure in an example Perl script that handles Facetted Clasification ( http://www.geocities.com/xtopicmaps/FacetedTaxonomy.html#code ):

$ListOfItems = {
'0001' => {name => $naam, category => ['Sport,SeaSport', 'Location,Island,Crete']},
'0002' => {name => 'Hotel Beta', category => ['Sport,SeaSport', 'Location,Mainland,Pilio']},
'0003' => {name => 'Hotel Gamma', category => ['Sport,WinterSport', 'Location,Mainland,Pilio']},
'0004' => {name => 'Hotel Delta', category => ['Sport,WinterSport', 'Sport,SeaSport', 'Location,Mainland,Pilio']},
'0005' => {name => 'Hotel Epsilon', category => ['Sport,WinterSport', 'Location,Mainland,Olympus']},
'0006' => {name => 'Hotel Zeta', category => ['Location,Mainland,Olympus']},
};

After this initial creation and initialization of the data structure, I'm trying to dynamically add elements to it from the contents of a SQL query. This is where I'm having problems though; the script does not the entry that is supposed to be created by following statement:

$ListOfItems{'0007'} = {name => 'Hotel Delta', category => ['Sport,WinterSport', 'Sport,SeaSport', 'Location,Mainland,Pilio']};

Can anyone tell me the correct syntax for adding elements to this data structure and supply me with additional information on what kind of data structure it is? I'm currently thinking its a hash of arrays, is this correct?

Thanks in advance.

silent11
08-27-2004, 10:54 AM
what kind of data structure it is? I'm currently thinking its a hash of arrays, is this correct?


I think this is a reference to a hash of hashes, however one of the values in the 'category' key is an unnammed array.

silent11
08-27-2004, 11:01 AM
I'm on a roll today :D

First off you copied the code wrong...

I changed the variable that you wanted to add so I could more clearly see it in in the data structure...




use strict;
use Data::Dumper;
# that's Data :: Dumper with no spaces

my $ListOfItems = {
'0001' => {name => 'Hotel Alpha', category => ['Sport,SeaSport', 'Location,Island,Crete']},
'0002' => {name => 'Hotel Beta', category => ['Sport,SeaSport', 'Location,Mainland,Pilio']},
'0003' => {name => 'Hotel Gamma', category => ['Sport,WinterSport', 'Location,Mainland,Pilio']},
'0004' => {name => 'Hotel Delta', category => ['Sport,WinterSport', 'Sport,SeaSport', 'Location,Mainland,Pilio']},
'0005' => {name => 'Hotel Epsilon', category => ['Sport,WinterSport', 'Location,Mainland,Olympus']},
'0006' => {name => 'Hotel Zeta', category => ['Location,Mainland,Olympus']},
};


$$ListOfItems{'0007'} = {name => 'Hotel Houston', category => ['Tennis,WinterSport', 'Sport,SeaSport', 'Location,Houston,Texas']};

print Dumper $ListOfItems;