Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by tybalt89 (Monsignor)
on Mar 17, 2021 at 01:15 UTC ( [id://11129808]=note: print w/replies, xml ) Need Help??


in reply to Re^6: 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.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11129741 use warnings; use List::Util qw( maxstr ); my %hash = ( '900000001' => { 'Network1' => [ 'Not used', 'Not used', 'Not used', 'Not used' ], 'Network1 ENV' => [ '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z' ], 'NETOWORK2' => [ '2021-03-10T18:15:54.000 +Z', '2020-12-17T19:36:21.000 +Z', '2021-03-10T18:15:54.000 +Z' ] }, '266000003' => { 'Network1 ENV' => [ '2021-03-08T13:34:5 +9.000Z' ] }, '566000003' => { 'Network1 ENV' => [ '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z', '2020-09-09T18:36:5 +0.000Z' ], 'NETOWORK2' => [ '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', '2021-03-10T18:15:54.000 +Z', '2021-03-10T18:15:54.000 +Z', '2020-12-17T19:36:21.000 +Z', '2021-03-10T18:15:54.000 +Z' ], 'Network1' => [ 'Not used', 'Not used' ] } ); #use Data::Dump 'dd'; dd \%hash; my %best; for my $id (keys %hash) { for my $net ( keys %{ $hash{$id} } ) { for ( @{ $hash{$id}{$net} } ) { /T/ and $best{$id} = maxstr "$_,$net", ($best{$id} // ''); } } } printf "ID: %s, Net Cat : %s , Valid: %s ", $_, reverse split /,/, $be +st{$_} for reverse sort keys %best;

Outputs:

ID: 900000001, Net Cat : NETOWORK2 , Valid: 2021-03-10T18:15:54.000Z I +D: 566000003, Net Cat : NETOWORK2 , Valid: 2021-03-10T18:15:54.000Z I +D: 266000003, Net Cat : Network1 ENV , Valid: 2021-03-08T13:34:59.000 +Z

Your expectation has an incorrect network and an incorrect datetime for 566000003

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 06:49 UTC

    I was still getting HASHrefferences like earlier

    my %ssoidmfadates; $VAR1 = { '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' ], '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' ], 'Network1' => [ 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA' ] }, '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' ] }, '266000003' => { 'Network1 ENV' => [ '2021-03-08T13:34:5 +9.000Z' ], 'Network1' => [ 'NA' ] } }; foreach $key (keys(%ssoidmfadates)) { foreach $net(keys %{ $ssoidmfadates{$key} }) { push @VALUES , $date_time = ParseDate((sort @{ $ssoidmfadates{$key} +{$net} })[$_]); my $date_time2 = maxstr (@VALUES); } }

    Can anyone let me know the reason i am not getting corrosponding $key and $net values by the above code? I am not getting my expected values.

    Please check my expected values one more time.

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

    I am getting below incorrect corrosponding value. How i can resolve this?

    266000003 Network1 ENV 2021-03-08T13:34:59.000Z 900000001 NETWORK2 2021-03-10T18:15:54.000Z 566000003 NETWORK2 2020-12-17T19:36:21.000Z
      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]

        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' ] } };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found