Hey Guys, thanks again for the help. I had another question if anyone could help me with.

The question is about assigning elements to a hash, and why is this certain record in the hash being overwritten instead of adding it to the hash?

So I have a text file with the following data (below). The idea is that a record could have only one 'OWNER' and could have any number of users who are 'WAITING' for that record ('RECORD_ID' is what links them between users who are OWNERS and WAITING because they will be the same).

INPUT-DATA
FILENAME RECORD_ID M INBR DNBR OWNER UNBR UNO TTY TIME DATE /ud/QC-DATA/CONTROL 001!SCHEDULE X 3462970 922337219576856 richard 500 +5444 156 pts/220 10:50:44 Sep 16 FILENAME RECORD_ID M INBR DNBR WAITING UNBR UNO TTY TIME DATE /ud/QC-DATA/CONTROL 001!SCHEDULE X 3462970 922337219576856 marshall 33 +09650 35 none 10:50:30 Sep 16 /ud/QC-DATA/CONTROL 001!SCHEDULE X 3462970 922337219576856 steven 4565 +899 122 pts/101 10:45:44 Sep 16 FILENAME RECORD_ID M INBR DNBR OWNER UNBR UNO TTY TIME DATE /ud/QC-PROD/NWO 667!NEWPRODUCT X 6468555 59761456123786 kevin 5555555 +999 pts/900 10:25:00 Sep 16 FILENAME RECORD_ID M INBR DNBR WAITING UNBR UNO TTY TIME DATE /ud/QC-PROD/NWO 667!NEWPRODUCT X 6468555 59761456123786 kelly 1234567 +886 none 10:28:00 Sep 16 /ud/QC-PROD/NWO 667!NEWPRODUCT X 6468555 59761456123786 russel 7654321 + 456 tty/101 10:25:00 Sep 16 FILENAME RECORD_ID M INBR DNBR OWNER UNBR UNO TTY TIME DATE /ud/NEW-PrOdUcTs/NWA 999?SheetMusic X 9876541 86555522211 mmartin 5151 +515 000 tty/100 10:51:00 Sep 16 FILENAME RECORD_ID M INBR DNBR WAITING UNBR UNO TTY TIME DATE /ud/NEW-PrOdUcTs/NWA 999?SheetMusic X 9876541 86555522211 luther 12345 +67 987 none 10:51:00 Sep 16

And here is my sub-routine that uses an array that contains all the data above in @temp (where each WHOLE line is an element in the array).

#@temp contains all data from input file (line-by-line) my @temp = <DATA>; my %records; my @fields; my $x = 0; if (@temp) { while ($x <= $#temp) { @fields = split " ", $temp[$x]; if ($fields[5] =~ /OWNER/) { $x++; @fields = split " ", $temp[$x]; print "OWNER --- @fields\n"; #Add OWNER to hash based on RECORD_ID $records{"$fields[1]"}{ OWNER } = { USER => "$fields[5]", FILENAME => "$fields[0]", PID => "$fields[6]", TIME => "$fields[9]", DATE => (join " ", "$fields[10] $fields[11]"), ELAPSED => [] }; #Increment to Next Line $x++; } else { #If line is WAITING header increment to next line. if ($fields[5] =~ /WAITING/) { $x++; } #Split line into array @fields = split " ", $temp[$x]; print "WAITING --- @fields\n"; #Add WAITING to hash based on RECORD_ID $records{"$fields[1]"}{ WAITING } = { USER => "$fields[5]", FILENAME => "$fields[0]", PID => "$fields[6]", TIME => "$fields[9]", DATE => (join " ", "$fields[10] $fields[11]"), ELAPSED => [] }; $x++; } } } print Dumper \%records; ____OUTPUT____ OWNER --- /ud/QC-DATA/CONTROL 001!SCHEDULE X 3462970 922337219576856 r +ichard 5005444 156 pts/220 10:50:44 Sep 16 WAITING --- /ud/QC-DATA/CONTROL 001!SCHEDULE X 3462970 922337219576856 + marshall 3309650 35 none 10:50:30 Sep 16 WAITING --- /ud/QC-DATA/CONTROL 001!SCHEDULE X 3462970 922337219576856 + steven 4565899 122 pts/101 10:45:44 Sep 16 OWNER --- /ud/QC-PROD/NWO 667!NEWPRODUCT X 6468555 59761456123786 kevi +n 5555555 999 pts/900 10:25:00 Sep 16 WAITING --- /ud/QC-PROD/NWO 667!NEWPRODUCT X 6468555 59761456123786 ke +lly 1234567 886 none 10:28:00 Sep 16 WAITING --- /ud/QC-PROD/NWO 667!NEWPRODUCT X 6468555 59761456123786 ru +ssel 7654321 456 tty/101 10:25:00 Sep 16 OWNER --- /ud/NEW-PrOdUcTs/NWA 999?SheetMusic X 9876541 86555522211 mm +artin 5151515 000 tty/100 10:51:00 Sep 16 WAITING --- /ud/NEW-PrOdUcTs/NWA 999?SheetMusic X 9876541 86555522211 +luther 1234567 987 none 10:51:00 Sep 16 $VAR1 = { '999?SheetMusic' => { 'WAITING' => { 'PID' => '1234567', 'TIME' => '10:51:00', 'DATE' => 'Sep 16', 'WAITERS' => [], 'FILENAME' => '/ud/NEW-PrOdUcTs/NWA', 'USER' => 'luther' }, 'OWNER' => { 'PID' => '5151515', 'TIME' => '10:51:00', 'DATE' => 'Sep 16', 'WAITERS' => [], 'FILENAME' => '/ud/NEW-PrOdUcTs/NWA', 'USER' => 'mmartin' } }, '667!NEWPRODUCT' => { 'WAITING' => { 'PID' => '7654321', 'TIME' => '10:25:00', 'DATE' => 'Sep 16', 'WAITERS' => [], 'FILENAME' => '/ud/QC-PROD/NWO', 'USER' => 'russel' }, 'OWNER' => { 'PID' => '5555555', 'TIME' => '10:25:00', 'DATE' => 'Sep 16', 'WAITERS' => [], 'FILENAME' => '/ud/QC-PROD/NWO', 'USER' => 'kevin' } }, '001!SCHEDULE' => { 'WAITING' => { 'PID' => '4565899', 'TIME' => '10:45:44', 'DATE' => 'Sep 16', 'WAITERS' => [], 'FILENAME' => '/ud/QC-DATA/CONTROL', 'USER' => 'steven' }, 'OWNER' => { 'PID' => '5005444', 'TIME' => '10:50:44', 'DATE' => 'Sep 16', 'WAITERS' => [], 'FILENAME' => '/ud/QC-DATA/CONTROL', 'USER' => 'richard' } } };

After executing the code above, I can see that the @fields, at the time of assigning elements to the hash, does give it ALL the elements of @temp (You can see that from the print statements of the array @fields). But it seems that which ever was the last 'WAITING' record processed, it will overwrite the one before it.
From the INPUT file you can see that RECORD_ID's "001!SCHEDULE" and "667!NEWPRODUCT" should each have 1 OWNER and 2 WAITING. And the RECORD_ID "999?SheetMusic" should have 1 OWNER and 1 WAITING.
Anyone know what I have to do for that not to overwrite the WAITING user before it?



Thanks in Advance,
Matt



In reply to Re: Perl Hashes, keys under keys (I think)? by mmartin
in thread Perl Hashes, keys under keys (I think)? by mmartin

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.