Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Using a hash to store complex data

by TJRandall (Sexton)
on Nov 24, 2009 at 16:34 UTC ( [id://809113]=perlquestion: print w/replies, xml ) Need Help??

TJRandall has asked for the wisdom of the Perl Monks concerning the following question:

I have to create a series of data extracts, based on unique unit Ids. Each unit Id can have one or multiple events related to it, and I was trying to use a hash of hashes to store the data. (running Perl 5.8.8)

When I run the code below, it is correct except for unit number 1234567890 - then I only get the 'PHB' event (and not the 'CNN' event)

What is the correct way to build out this data storage? I know that the inner hash is not allowing the duplicate events, so does that mean I have to have a hash of arrays?

+---------------------------------------+ | | Event | Date/Time 1 | | | CNN | Date/Time 2 | | | | (etc) | | +-----------+------------- | | | Event | Date/Time 1 | | unique | PHB | Date/Time 2 | | unit # | | (etc) | | +-----------+------------- | | | Event | Date/Time 1 | | | CL | Date/Time 2 | | | | (etc) | | +-----------+------------- | | | Event | Date/Time 1 | | | SWAP | Date/Time 2 | | | | (etc) | +------------+-----------+--------------+ | | Event | Date/Time 1 | | | CNN | Date/Time 2 | | | | (etc) | | +-----------+------------- | | another | Event | Date/Time 1 | | unique | PHB | Date/Time 2 | | unit # | | (etc) | | +-----------+------------- | | | Event | Date/Time 1 | | | CL | Date/Time 2 | | | | (etc) | | +-----------+------------- | | | Event | Date/Time 1 | | | SWAP | Date/Time 2 | | | | (etc) | +---------------------------------------+
Thank you for any help that you are able to provide!!

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use vars qw(%HoH $field $unitNum $timestmp %hash $key); # seed the hash with some data %HoH = ( 8675309121 => { SWAPS => ["11/20/2009 12:43:00"], }, 5432129876 => { CNM => [ "11/20/2009 00:00:00" ], PHB => [ "11/20/2009 00:01:00", "11/20/2009 00:02:00" ], }, ); print Dumper( %HoH ); # reads sample data while(<DATA>){ # get a line of data chomp; my ($unit_num, $event_nm, $event_date) = split(/,/); %hash = ( $unit_num => { $event_nm => [ $event_date ], }, ); @HoH{keys %hash} = values %hash } print Dumper( %HoH ); __END__ 1234567890,CNN,11/20/2009 00:01:00 1234567890,PHB,11/20/2009 00:01:02 8887776543,CL,11/20/2009 11:09:00

Replies are listed 'Best First'.
Re: Using a hash to store complex data
by ikegami (Patriarch) on Nov 24, 2009 at 16:48 UTC

    %hash only has one key (how useless!), so

    @HoH{keys %hash} = values %hash
    is the same as
    $HoH{$unit_num} = $hash{$unit_num};

    As you can clearly see now, you completely replace all existing data for $unit_num every time you find a new record for $unit_num.

    Change

    %hash = ( $unit_num => { $event_nm => [ $event_date ], }, ); @HoH{keys %hash} = values %hash
    to
    push @{ $HoH{$unit_num}{$event_nm} }, $event_date
    Thanks to autovivification, the above is short for
    $HoH{$unit_num} ||= {}; $HoH{$unit_num}{$event_nm} ||= []; push @{ $HoH{$unit_num}{$event_nm} }, $event_date;

    Tip: Don't pass hashes and arrays to Dumper. Pass references to them:

    print Dumper( \%HoH );
Re: Using a hash to store complex data
by toolic (Bishop) on Nov 24, 2009 at 17:04 UTC
Re: Using a hash to store complex data
by Anonymous Monk on Nov 24, 2009 at 16:50 UTC
    change
    %hash = ( $unit_num => { $event_nm => [ $event_date ], }, ); @HoH{keys %hash} = values %hash
    to
    push @{$HoH{$unit_num}{$event_nm}}, $event_date;
Re: Using a hash to store complex data
by TJRandall (Sexton) on Nov 24, 2009 at 20:28 UTC

    Thank you, Monks! I appreciate the help!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://809113]
Approved by keszler
Front-paged by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found