Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^9: Unable to set the recent date from hash of array of hashes.

by choroba (Cardinal)
on Mar 17, 2021 at 14:33 UTC ( [id://11129831]=note: print w/replies, xml ) Need Help??


in reply to Re^8: Unable to set the recent date from hash of array of hashes.
in thread Unable to set the recent date from hash of array of hashes.

say can be enabled by
use feature qw{ say };

But I think I know understand your problem. You want to get the maximum date and the corresponding net for each key, i.e. you aren't searching for a maximum date for each key and net.

There are several ways how you can proceed. For example, you can first find the max date, then walk the structure and pick the paths that lead to the max date. If there's the same date for several different nets, all of them will be reported:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::Util qw{ maxstr }; my %ssoidmfadates = ( ... ); for my $key (keys %ssoidmfadates) { my $last_date = maxstr(grep 'NA' ne $_, map values @$_, values %{ +$ssoidmfadates{$key} }); my @dates; for my $net (keys %{ $ssoidmfadates{$key} }) { push @dates, $net if grep $_ eq $last_date, @{ $ssoidmfadates{ +$key}{$net} }; } say "$key\t$_\t$last_date" for @dates; }

Or, if the data is large and you only want to iterate it once, you can instead

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my %ssoidmfadates = ( ... ); for my $key (keys %ssoidmfadates) { my $max_date = ""; my %max_net; undef $max_net{ (keys %{ $ssoidmfadates{$key} })[0] }; for my $net (keys %{ $ssoidmfadates{$key} }) { for my $date (@{ $ssoidmfadates{$key}{$net} }) { next if 'NA' eq $date || $date lt $max_date; if ($max_date lt $date) { $max_date = $date; %max_net = (); } undef $max_net{$net}; } } say "$key\t$_\t$max_date" for keys %max_net; }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^10: Unable to set the recent date from hash of array of hashes.
by chandantul (Scribe) on Mar 21, 2021 at 03:31 UTC

    Thanks for all your help Sir, I have resolved the write XLSX issue and all are good.

Re^10: Unable to set the recent date from hash of array of hashes.
by chandantul (Scribe) on Mar 17, 2021 at 19:38 UTC

    Thank you Sir, This is my current output and the below code works but unable to write the output in an excel

    Unable to write in exceland need your help is that 'say' creating issue here?

    My current output

    NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z Network1 ENV 2021-03-08T13:34:59.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2021-03-10T18:15:54.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z NETWORK2 2020-12-17T19:36:21.000Z

    My current script

    foreach my $p (0..$#cellC ) { if(exists $ssoidmfadates{$cellC[$p]}){ my $max_date = ""; my %max_net; undef $max_net{ (keys %{ $ssoidmfadates{$cellC[$p]} })[0] }; for my $net (keys %{ $ssoidmfadates{$cellC[$p]} }) { for my $date (@{ $ssoidmfadates{$cellC[$p]}{$net} }) { #for my $date (@{ $ssoidmfadates{$key}{$net} }) { next if 'NO Used' eq $date || $date lt $max_date; if ($max_date lt $date) { $max_date = $date; %max_net = (); } undef $max_net{$net}; } } my $fact1 = say "$_\t" for keys %max_net; my $date_time3 = say "$max_date\t" for keys %max_net; print "$fact1" , "$date_time3"; $worksheet->write($r17, 0, $mfafact1 ); $worksheet->write($r18, 1, $date_time3); } else { print "Not Exist"; } $r17 += 1; $r18 +=1; }
    $VAR1 = { '900000001' => { 'NETWORK2' => [ '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z', '2020-12-17T19:36:21.000 +Z' ] }, '566000003' => { 'Network1 ENV' => [ '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z' ], 'Network1' => [ 'NO Used', 'NO Used', 'NO Used', 'NO Used', 'NO Used', 'NO Used', 'NO Used', 'NO Used', 'NO Used' ], 'NETWORK2' => [ '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z' ] }, '266000003' => { 'Network1' => [ 'NO Used' ], 'Network1 ENV' => [ '2021-03-08T13:34:5 +9.000Z' ] } };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129831]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found