Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by choroba (Cardinal)
on Mar 17, 2021 at 10:10 UTC ( [id://11129822]=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.

Your code doesn't return nor output anything.

If ParseDate is coming from Date::Manip, I don't think it can handle a list of dates.

Also, what's the value of $_?

Also, why do you need to sort the dates? The following outputs the maximal date for each net and key:

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

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

    Sir,Thanks.This is my current output

    Unable to get maximum single values for writing in excel. Please help if possible.We need to check the users that have Network access and their corrosponding date of access, if they have not access but having network access print NO Used. Unable to use "say" as i was getting error "Undefined subroutine &main::say called"

    Joined String is 900000001 NETWORK2 2020-12-17T19:36:21.000Z Joined String is 566000003 Network1 ENV 2020-09-09T18:36:50.000 +Z Joined String is 566000003 NETWORK2 2021-03-10T18:15:54.000Z Joined String is 266000003 Network1 ENV 2021-03-08T13:34:59.000 +Z

    My expected output

    Joined String is 900000001 NETWORK2 2020-12-17T19:36:21.000Z Joined String is 566000003 NETWORK2 2021-03-10T18:15:54.000Z Joined String is 266000003 Network1 ENV 2021-03-08T13:34:59.000 +Z

    My current script

    for my $key (keys %ssoidmfadates) { for my $net (keys %{ $ssoidmfadates{$key} }) { my @dates = grep 'NO Used' ne $_, @{ $ssoidmfadates{$key}{$net +} }; next unless @dates; my $date3 = maxstr(@dates); #say (join "\t", $key, $net, maxstr(@dates)); #my $string = join "\t", $key, $net, maxstr(@dates); my $string = join "\t", $key, $net, $date3; print"Joined String is $string\n"; #print "SSO ID: $key, Mfa Cat : $net , Valid: $date_time2\n"; } }
    $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' ] } };
      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]

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

        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://11129822]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-24 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found