Click to See Complete Forum and Search --> : [RESOLVED] Hash/array question - adding arrays to hash


gnetcon
08-25-2006, 01:57 PM
Everyone:

I admittedly come from a PHP background, and am trying to convert a script in PHP to run in Perl. I have it all running, except for one piece.

In my code, I loop through a MySQL query to create RSS files for items in a database. Everything works great. But in my PHP code, I also created a RSS file for each state with items that belonged to it. So my array may look like:


$arrStates["md"][0]["lid"] = "Name"
$arrStates["md"][0]["title"] = "Title Here"
$arrStates["md"][1]["lid"] = "Name #2"
$arrStates["md"][1]["title"] = "Title #2 Here"
$arrStates["de"][0]["lid"] = "Name"
$arrStates["de"][0]["title"] = "Title Here"
$arrStates["de"][1]["lid"] = "Name #2"
$arrStates["de"][1]["title"] = "Title #2 Here"


I did that by doing:


$arrStates[$state][] = array("lid" => "Name", "title" = "Title Here");


What is the best way to do this in Perl? Should I have a hash (%arrStates{$state}) that I push an array onto? Or is this nonsense? Is there a better way to do it?

TIA!

gnetcon
08-25-2006, 03:34 PM
I can happily say I got this working on my own. In case anyone has this same issue, this is what I did:


$arrStates{ $state }{ $lid }{ "title" } = "Title";
$arrStates{ $state }{ $lid }{ "message" } = "Message";
$arrStates{ $state }{ $lid }{ "link" } = "URL";
$arrStates{ $state }{ $lid }{ "author" } = "Author";
$arrStates{ $state }{ $lid }{ "date" } = "Date";


Then, to loop through, I did the following:


for my $state ( sort keys %arrStates ) {
# loop through our lid's and add each item
for my $lid ( sort keys %{%arrStates->{ $state }} ) {
print "Title => " . %arrStates->{ $state }->{ $lid }->{ "title" } . "\n";
etc...
}
}