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

In reply to Using a hash to store complex data by TJRandall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.