Hello Monks,

I'm new to Perl and got into a problem which I need your help to solve. I have a hash like below (using Dumper)

print '%ERROR1: ' .Dumper \%ERROR1; 'Oracle' => { 'server1' => { 'BACKUP ALERT' => { 'D +AILY' => { + '1433471420' => 1 + } }, 'FILESYSTEM ALERT' => { 'WARNING 8 +5% FULL' => { + '1433523904' => 1, + '1433520305' => 1, + '1433451904' => 1, + '1433450109' => 1, + '1433455504' => 1, + '1433509506' => 1, + '1433529308' => 1, + '1433534713' => 1 + } + }, }, 'server2' => { 'FILESYSTEM ALERT' => { +'WARNING 85% FULL' => { + '1433523635' => 1, + '1433455225' => 1, + '1433498427' => 1, + '1433512846' => 1, + '1433534424' => 1 + } }, + .. ... ..

This is a big hash with thousands of records, this has all the server alerts and diferrent timestamps that occurred. The 4th element is the timestamp.

I want to capture the last timestamp value occurrence for each server's generated alerts. something that should look like below

group Hostname Service Count State + Alert Started Alert Duration Oracle server1 BACKUP ALERT 1 DAILY + 1433471420 10 MINUTES Oracle server1 FILESYSTEM ALERT 8 WARNING 85% FULL + 1433523904 1 HOUR Oracle server2 .. ..

I'm able to capture the group, hostname, service, count and state from the hash using map (as follows) but do not know how to capture the first and last timestamp values. I've defined $ts1 and $ts2 for the same.

my @LIST = map { [ $_->[0], $_->[1], $_->[2], $_->[3], $_->[4], $_->[5],$_->[6] + ] } sort { $a->[0] cmp $b->[0] || $b->[4] <=> $a->[4] || $a->[1] cmp $b-> +[1] || $a->[2] cmp $b->[2] || $a->[3] cmp $b->[3] } map { my $group = $_; map { my $host = $_; map { my $service = $_; map { my $state = $_; my $count = keys %{$ERROR1{$group}->{$host}->{ +$service}->{$state}}; my $ts1 = (sort (keys %{$ERROR1{$group}->{$hos +t}->{$service}->{$state}}))[0]; my $ts2 = (sort(keys %{$ERROR1{$group}->{$host +}->{$service}->{$state}}))[$count]; [ $group, $host, $service, $state, $count, $ts +1, $ts2 ] } keys %{$ERROR1{$group}->{$host}->{$_}} } keys %{$ERROR1{$group}->{$_}} } keys %{$ERROR1{$_}} } keys %ERROR1;

The output of the above map is as below, I'm able to get the first occurrence of timestamp, but I want to get the last occurrence of the timestamps on the instead of "undef" value using the count mentioned above.

[ 'Oracle', 'server1', 'FILESYSTEM ALERT', 'WARNING 85% FULL', 8, '1433521920', { '' => undef } ], [ -- -

Any help will be greatly appreciated.


In reply to Getting first and last element value of a hash loop by techworkpc

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.