Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: How i can create hash of arrays of hashes from my 3 arrays?

by BillKSmith (Monsignor)
on Mar 14, 2021 at 23:47 UTC ( #11129631=note: print w/replies, xml ) Need Help??


in reply to Re^2: How i can create hash of arrays of hashes from my 3 arrays?
in thread How i can create hash of arrays of hashes from my 3 arrays?

Your new specification makes little or no sense. I think you want to print the hash data for the users specified in @array4. Here is my attempt at that.
foreach my $id (@array4) { print "User $id\n"; if (!exists $hash{$id}) { print " not logged in\n\n"; next; } foreach my $net (keys %{$hash{$id}}) { print " $net\n"; local $" = "\n "; print " @{$hash{$id}{$net}}\n"; } print "\n"; }
OUTPUT: User 900000001 Network1 Login User 900000002 Network1 2019-11-07T20:31:10.000Z 2021-02-19T13:47:25.000Z 2021-03-10T13:58:50.000Z User 900000007 Network1 2021-03-10T17:11:24.000Z 2021-02-18T08:34:58.000Z 2021-01-25T17:19:44.000Z User 900000009 not logged in User 900000010 not logged in User 900000011 not logged in User 900000012 not logged in User 500000008 Network1 2021-03-10T17:11:24.000Z 2021-03-11T17:12:42.000Z Network2 Not Used Network3 Not Used
Bill

Replies are listed 'Best First'.
Re^4: How i can create hash of arrays of hashes from my 3 arrays?
by chandantul (Scribe) on Mar 15, 2021 at 01:23 UTC

    Hello Bill, This is very nice that you are helping me here. Thanks for the same. I was able to create the hash of arrays and hashes but unable detirmine the recent dates from users

    My expectation should be 1 recent date 2021-03-10T13:58:50.000Z . Please check below code, i was trying

    User 900000002 Network1 2019-11-07T20:31:10.000Z 2021-02-19T13:47:25.000Z 2021-03-10T13:58:50.000Z
    my %iddates; for my $key (keys(%iddates)) { foreach my $fact1 (keys %{ $iddates{$key} }) { $iddates{$key}{$fact1}; my @numbers = map { $_->[1] } values(%{$iddates{$key}}); printf("%dd: max: %s", $key, maxstr(@numbers)); } }
      Your follow-up questions and sample code indicate that you are attempting a project far beyond your skill level. I strongly suggest that you get a copy of the book 'Learning Perl". Start at the beginning and do every exercise. The ones that are easy will waste very little time. The hard ones will show you where study is required. As you study, it is as important to learn the correct Perl names for things as it is to learn the coding techniques. Of course, you can ask an occasional question here, but remember that the idea is to learn by doing it yourself.
      Bill
      ... detirmine the [most] recent dates from users

      The date-timestamps are held in the anonymous array references that are the values of the key(s) of $hash{$id}{$net}. As suggested by choroba here, the particular format of these date-timestamps is easily sort-ed to determine temporal order:

      Win8 Strawberry 5.8.9.5 (32) Sun 03/14/2021 21:56:16 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my %hash; $hash{'ID'}{'NET'} = [ qw( 2019-11-07T20:31:10.000Z 2021-03-10T13:58:50.000Z 2021-02-19T13:47 +:25.000Z ) ]; my $most_recent_date_time = (sort @{ $hash{'ID'}{'NET'} })[-1]; print "'$most_recent_date_time' \n"; ^Z '2021-03-10T13:58:50.000Z'
      Try some other date-timestamps (with the identical format!) and convince yourself this is true.

      (And congratulations on having had an almost-complete solution finally delivered into your hands! Please see the Offering Plate. :)


      Give a man a fish:  <%-{-{-{-<

        Hello Bill, Thanks for helping me but i have the issue with multiple entries of the dates for different key. I have following data structure in my hash but i will need only one entry for the date and time which is more recent among all the respective keys and values but i am 2 values - one min and maximum. I will need one value instead. How i can achive?

        My expectation should be below

        ID: 900000001, Net Cat : NETOWORK2 , Valid: 2021-03-10T18:15:54.000Z ID: ID: 566000003, Net Cat : NETOWORK2 , Valid: 2021-03-10T18:15:54.000Z ID: 266000003, Net Cat : Network1 ENV , Valid: 2021-03-08T13:34:59.000Z
        my %hash = $VAR1 = { '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' ] } };

        my code snippet below in order to find most recent date for the user among all the keys

        foreach my $key (keys(%hash)) { foreach my net (keys %{ $hash{$key} }) { my $date_time = (sort @{ $ssoidmfadates{$key}{$net} })[-1]; my @dt_dob1 = unpack("A4xA2xA2",$date_time); if (check_date(@dt_dob1)){ print "SSO ID: $key, Mfa Cat : $net , Valid: $date_time\n +"; } else { print "Not used"; } } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-05-28 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?