Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Hashes of Hashes of Arrays? Is it possible?

by liverpole (Monsignor)
on Sep 08, 2011 at 18:49 UTC ( #924906=note: print w/ replies, xml ) Need Help??


in reply to Hashes of Hashes of Arrays? Is it possible?

Hi capriguy84,

Looks to me like you've got a simple typo ... first you called the data structure %HOH, then later you referred to it as %HoH. One other typo in your code is the extra quotation mark (or "double quotes" character) on this line:

pal => "[ "george", "jane", "elroy" ],

Fixing those 2 things, giving explicit values for $key and $value, and using Data::Dumper to check the results:

use strict; use warnings; use Data::Dumper; my %HoH = ( flintstones => { lead => "fred", pal => [ "george", "jane", "elroy" ], }, jetsons => { lead => "george", wife => [ "george", "jane", "elroy" ], boy => [ "george", "jane", "elroy" ], }, simpsons => { lead => "homer", wife => [ "george", "jane", "elroy" ], kid => [ "george", "jane", "elroy" ], }, ); my $key = 'flintstones'; my $value = 'Wilma'; printf "Before: %s\n", Dumper(\%HoH); $HoH{$key}{"name"} = $value; printf " After: %s\n", Dumper(\%HoH); __END__ Output (Note that 'Wilma' was successfully added): Before: $VAR1 = { 'simpsons' => { 'kid' => [ 'george', 'jane', 'elroy' ], 'lead' => 'homer', 'wife' => [ 'george', 'jane', 'elroy' ] }, 'jetsons' => { 'lead' => 'george', 'wife' => [ 'george', 'jane', 'elroy' ], 'boy' => [ 'george', 'jane', 'elroy' ] }, 'flintstones' => { 'lead' => 'fred', 'pal' => [ 'george', 'jane', 'elroy' ] } }; After: $VAR1 = { 'simpsons' => { 'kid' => [ 'george', 'jane', 'elroy' ], 'lead' => 'homer', 'wife' => [ 'george', 'jane', 'elroy' ] }, 'jetsons' => { 'lead' => 'george', 'wife' => [ 'george', 'jane', 'elroy' ], 'boy' => [ 'george', 'jane', 'elroy' ] }, 'flintstones' => { 'lead' => 'fred', 'name' => 'Wilma', 'pal' => [ 'george', 'jane', 'elroy' ] } };

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/


Comment on Re: Hashes of Hashes of Arrays? Is it possible?
Select or Download Code
Re^2: Hashes of Hashes of Arrays? Is it possible?
by capriguy84 (Novice) on Sep 08, 2011 at 19:07 UTC
    Thanks for your reply. I am reading a CSV file(below) and creating a temporary complex hash structure as described to perform some operations. As you can see my $key is the time which should be only one per second and plug the data as hash/array.
    NAME,06/01/2011,09:30:00,16.76,756,Q,00 NAME,06/01/2011,09:30:00,16.76,300,Q,00 NAME,06/01/2011,09:30:00,16.76,100,Q,00 NAME,06/01/2011,09:30:01,16.76,100,Q,00 NAME,06/01/2011,09:30:01,16.76,200,Z,00 NAME,06/01/2011,09:30:02,16.77,200,X,00 NAME,06/01/2011,09:30:02,16.77,200,X,00

    Open 100k line CSV file and read using Text_CSV. I have to undef the hashes before jumping to next second.
    my $csv = Text::CSV_XS->new(); open (FILE, "<", "$tickdir/$file") or die "Can't open CSV File:$! +\n"; while (<FILE>) { $csv->parse($_); my @columns = $csv->fields(); my $key = str2time($columns[1].' '.$columns[2]); my (@prices, @volumes) = (); my %HOH = (); $HOH{$key}{"name"} = $columns[0]; push(@{$HOH{$key}{"price"}}, $columns[3]); push(@{$HOH{$key}{"volumes"}}, $columns[4]); print Dumper(\%HOH); } close FILE;
    OUTPUT: As you can see the key values are same.
    $VAR1 = { '1306935005' => { 'volumes' => [ '200' ], 'name' => 'NAME', 'price' => [ '16.76' ] } }; $VAR1 = { '1306935005' => { 'volumes' => [ '400' ], 'name' => 'NAME', 'price' => [ '16.76' ] } };
      You are overwriting your hash every time through the while loop. Move the declaration of the hash outside of the loop:
      my $csv = Text::CSV_XS->new(); open (FILE, "<", "$tickdir/$file") or die "Can't open CSV File:$! +\n"; my %HOH; while (<FILE>) { $csv->parse($_); my @columns = $csv->fields(); my $key = str2time($columns[1].' '.$columns[2]); my (@prices, @volumes) = (); $HOH{$key}{"name"} = $columns[0]; push(@{$HOH{$key}{"price"}}, $columns[3]); push(@{$HOH{$key}{"volumes"}}, $columns[4]); print Dumper(\%HOH); } close FILE;
        ahh..That fixed it. Thank you very much. One quick question, how do I avoid having to save the huge dataset in memory by processing and printing the results on fly? I would like to undef the HASH at after each second in time i.e create a loop for each key and undef my %HOH. Any ideas how/where I can add the control statement for $key?
        Jim, Is there a Perl specific control break? Or should I just permutate various loop control statements like unless, continue etc.?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://924906]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2015-07-07 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls