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?
Thank you for any help that you are able to provide!!+---------------------------------------+ | | 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) | +---------------------------------------+
#!/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 | |
|
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 | |
|
Re: Using a hash to store complex data
by TJRandall (Sexton) on Nov 24, 2009 at 20:28 UTC |