> Hi, > > I have the following structure : > > $hash{$date} = { > 'ip' => $ip, > 'action' => $action, > }; > > witch produce data like : > > $VAR1 = '[15/Jul/2015:10:30:03 +0200]'; > $VAR2 = { > 'ip' => 'xxx.xxx.xxx.xxx', > 'action' => 'GET xxx' > }; > > and an array of ip addresses, say @ip > > My question is how can I display the content of %hash in the order of > @ip, assuming %hash has the same length as @ip ? > > Thank you >
Maybe this might be helpful: Code: #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Original data sample my %bydate = ( "12/01/2014" => { 'ip' => '127.0.0.1', 'action' => 'GET foo' }, "11/04/2013" => { 'ip' => '192.168.1.10', 'action' => 'PUT bar' }, "10/08/2012" => { 'ip' => '10.10.8.6', 'action' => 'POST baz' }, ) ; # List of IPs in desired print order; sort however you like my @ips = ('192.168.1.10', '10.10.8.6', '127.0.0.1'); print "\nOriginal hash:\n"; print Dumper(\%bydate); my %byip = map {($bydate{$_}{ip}, {action => $bydate{$_}{action}, date => $_})} (keys %bydate); print "\nHash keyed by IP address\n"; print Dumper \%byip; print "\n"; print "\nPrinting out by IP:\n"; foreach my $ip (@ips) { print "IP: $ip Date: $byip{$ip}{date} Action: $byip{$ip}{action}\n"; } Output I received: Original hash: $VAR1 = { '11/04/2013' => { 'action' => 'PUT bar', 'ip' => '192.168.1.10' }, '12/01/2014' => { 'action' => 'GET foo', 'ip' => '127.0.0.1' }, '10/08/2012' => { 'action' => 'POST baz', 'ip' => '10.10.8.6' } }; Hash keyed by IP address $VAR1 = { '127.0.0.1' => { 'date' => '12/01/2014', 'action' => 'GET foo' }, '10.10.8.6' => { 'date' => '10/08/2012', 'action' => 'POST baz' }, '192.168.1.10' => { 'action' => 'PUT bar', 'date' => '11/04/2013' } }; Printing out by IP: IP: 192.168.1.10 Date: 11/04/2013 Action: PUT bar IP: 10.10.8.6 Date: 10/08/2012 Action: POST baz IP: 127.0.0.1 Date: 12/01/2014 Action: GET foo Best of luck Nathan -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/