http://www.perlmonks.org?node_id=952354


in reply to add data to HoA

Let's break it down:

push (@ {$GSPmsgHash{$date} }, @days_between);

1. $GSPmsgHash{$date}

This gives the arrayref for key $date of the hash $GSPmshHash

2. @{ $GSPmsgHash{$date} }

Now you're dereferencing the arrayref and getting the array

3. push (@ {$GSPmsgHash{$date} }, @days_between);

So now you push two the two elements from @days_between onto the array referenced by $GSPmsgHash{$date}.

So the result you're seeing is completely expected. Where were you trying to add the @days_between data?

Replies are listed 'Best First'.
Re^2: add data to HoA
by dbs (Sexton) on Feb 07, 2012 at 22:03 UTC
    Thank you for that clear explanation! I am trying to get the 2 elements from days_between added on to each key which is my date. So it should look like this after some editing:
    $VAR1 = { DATE: 01/20/2012 TIME: 21:12:38 ' => [ ALERT LEVEL: 6 = required ', SOURCE: 4 = power ' ', SOURCE DETAIL: 4 = ', PROBLEM DETAIL: 7 = ' ', 18, ], + DATE: 02/05/2012 TIME: 07:50:19 ' => [ ALERT LEVEL: 2 = ', SOURCE: 0 = ' ', SOURCE DETAIL: 0 = ', PROBLEM DETAIL: 0 = ' ' 2 ] };
    notice the 18 and 2 are in the proper key/value pairs based on date subtraction from the log from today.

      You'd have to push each element of @days_between onto each of the arrays in $GSPmsgHash like so:

      push @{$GSPmsgHash{$date1}}, $days_between[0]; push @{$GSPmsgHash{$date2}}, $days_between[1];

      You'd have to figure out the respective keys ($date1 and $date2) you'd need for each of the values in @days_between so you can add the right value to the right array.

        I decided to change my code around a bit, so now I am not seeing a value in my HoH when I print normally, but when I print using Data Dumper I see it??? The item I am NOT seeing is the number 3 under __OUT__. Help! thnkU!
        __DUMPER OUT__ $VAR1 = { DATE: 01/20/2012 TIME: 21:12:38 ' => { ALERT LEVEL: 6 = Boot possible, pending failure - action required SOURCE: 4 = power ', SOURCE DETAIL: 4 = high voltage DC power SOURCE ID: FF PROBLEM DETAIL: 7 = illegal power supply type ', '19' => ' Days Bet +ween Today and last Alert' }, DATE: 02/05/2012 TIME: 07:50:19 ' => { SOURCE DETAIL: 0 = unknown, no source stated SOURCE ID: FF PROBLEM DETAIL: 0 = no problem detail ', '3' => ' Days Betw +een Today and last Alert', ALERT LEVEL: 2 = Non-Urgent operator attention required SOURCE: 0 = unknown, no source stated ' } }; __OUT__ # perl HPMP_mark3_parselogs.plx DATE: 01/20/2012 TIME: 21:12:38 ==> ALERT LEVEL: 6 = Boot possible, pending failure - action required SOURCE: 4 = power SOURCE DETAIL: 4 = high voltage DC power SOURCE ID: FF PROBLEM DETAIL: 7 = illegal power supply type 19 Days Between Today and last Alert DATE: 02/05/2012 TIME: 07:50:19 ==> SOURCE DETAIL: 0 = unknown, no source stated SOURCE ID: FF PROBLEM DETAIL: 0 = no problem detail ALERT LEVEL: 2 = Non-Urgent operator attention required SOURCE: 0 = unknown, no source stated __ACTUAL_CODE__ use strict; use warnings; use FileHandle; use MIME::Lite; use Date::Calc "Delta_Days", "Today"; my $RLOG = new FileHandle "+< $Rawlog" or warn "Open failed: $!"; my (@ary, $recordcount, $alert4orMoreCount, @attnlight, $dateofalert, @dateofalert, $arranged, $days_between, @days_between, $attnlightcount, @dates, $alert3orLessCount, %GSPmsgHash, $date, $alertLv, $src, $srcD, $pr +obD ); my ( $year,$month,$day ) = Today(); while (<$RLOG>) { if (/(Log Entry\s\#.*)/) { ++$recordcount; my $log = $1 .+ "$RTF{ln2}"; my $sys = <$RLOG>; $date = <$RLOG>; $alertLv = <$RLOG>; <$RLOG>; $src = <$RLOG>; $srcD = <$RLOG>; $probD = <$RLOG>; <$RLOG>; my $callerA = <$RLOG>; my $callerAS = <$RLOG>; my $rptEnt = <$RLOG>; <$RLOG>; my $hex1 = <$RLOG>; my $hex2 = <$RLOG> .+ "$RTF{ln}=========================== +==============="; push @ary, ($log,$sys,$date,$alertLv,$src,$srcD, $probD,$callerA,$callerAS,$rptEnt,$hex1,$hex2 ); if (/(LEDs:\s+RUN.*)/) { my $attnlight = <$RLOG>; push @attnlight, (split(" ", $attnlight, 0))[1]; } $dateofalert = ((split (" ", $date, 0))[1]); $dateofalert =~ y /\///d; $arranged = substr( $dateofalert, -4, 4 ) . substr( $dateof +alert, 0, 4); my $yyyy = substr( $arranged, 0, 4); my $mm = substr( $arranged, -4, 2); my $dd = substr( $arranged, -2); $days_between = Delta_Days($yyyy, $mm, $dd, $year, $month, $da +y); $GSPmsgHash{$date} = { $alertLv, $src, $srcD, $probD, $days_between, ' Days Between Today and last Alert', } } } $RLOG->close; while ( my ($k, $v) = each(%GSPmsgHash) ) { print "\n$k ==>\n",%{$v},"\n"; }