Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

add data to HoA

by dbs (Sexton)
on Feb 07, 2012 at 21:05 UTC ( #952347=perlquestion: print w/replies, xml ) Need Help??

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

Trying to add data to my HoA using code below, but its appending 18,2 to the first key/val pair. Here is my HoA variable structure:
$GSPmsgHash{$date} = [ $alertLv,$src,$srcD,$probD ];
@days_between contains 2 elements: 18 and 2 or days between 2 diff dates from today.
push (@ {$GSPmsgHash{$date} }, @days_between); # perl HPMP_mark3_parselogs.plx 2 # of elements: [ 18, 2 ] $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 ' ', 18, 2 ], DATE: 02/05/2012 TIME: 07:50:19 ' => [ ALERT LEVEL: 2 = Non-Urgent operator attention required ', SOURCE: 0 = unknown, no source stated ' ', SOURCE DETAIL: 0 = unknown, no source stated SOURCE ID: FF ', PROBLEM DETAIL: 0 = no problem detail ' ' ] };
Please help! thanks!!!

Replies are listed 'Best First'.
Re: add data to HoA
by Riales (Hermit) on Feb 07, 2012 at 21:29 UTC

    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?

      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.

Re: add data to HoA
by Anonymous Monk on Feb 07, 2012 at 21:17 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://952347]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2021-04-23 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?