techworkpc has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting first and last element value of a hash loop
by FreeBeerReekingMonk (Deacon) on Jun 05, 2015 at 22:15 UTC | |
by FreeBeerReekingMonk (Deacon) on Jun 05, 2015 at 22:29 UTC | |
by techworkpc (Initiate) on Jun 05, 2015 at 23:01 UTC | |
|
Re: Getting first and last element value of a hash loop
by aaron_baugher (Curate) on Jun 05, 2015 at 22:01 UTC | |
by parv (Parson) on Jun 05, 2015 at 22:06 UTC | |
|
Re: Getting first and last element value of a hash loop
by Laurent_R (Canon) on Jun 06, 2015 at 11:16 UTC |