Why you don't want rebuild you hash into another hash with ip address as a key?
Yes, it will cost you additional memory and runtime but it will be more convenient and easy to understand, support and modificate. If memory is not a problem, i think it's best choice in your case. See https://gist.github.com/elcamlost/dad41b61321c72b76563 as simple example. пн, 20 июля 2015 г. в 10:21, Vincent Lequertier <s...@riseup.net>: > Thank you for the help, but this does not work. We needa pass the ip > addresses to the sorting function, because actually the keys of the hash > are the dates > > $VAR1 = '[15/Jul/2015:10:30:03 +0200]'; > $VAR2 = { > 'ip' => 'xxx.xxx.xxx.xxx', > 'action' => 'GET xxx' > }; > > The workaround I found is to loop over the hash, push an array with the > ip addresses, and sort them, like this : > > sub sort_by_ip { > my @ip; > for my $key (keys %hash) { > push @ip, $hash{$key}{ip}; > } > my @ip_sorted = map { $_->[0] } > sort { $a->[1] <=> $b->[1] } > map { [$_, int sprintf("%03.f%03.f%03.f%03.f", > split(/\./, $_))] } @ip; > } > > So I'm looking for a way to iterate through the hash in the order of my > array. > > Regards > -- > Vincent Lequertier > vincentlequertier.tk > > Le 2015-07-17 15:50, Shawn H Corey a écrit : > > On Fri, 17 Jul 2015 15:11:13 +0200 > > Vincent Lequertier <s...@riseup.net> wrote: > > > >> 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 > >> > > > > UNTESTED: > > > > sub ip_cmp { > > my @ip_a = split /\./, $a; > > my @ip_b = split /\./, $b; > > > > for my $i ( 0 .. 3 ){ > > my $cmp = ( $ip_a[$i] || 0 ) <=> ( $ip_b[$1] || 0 ); > > return $cmp if $cmp != 0; > > } > > return 0; > > } > > > > for my $hash_ref ( sort ip_cmp keys %hash ){ > > # display $hash_ref->{$date} > > } > > > > > > -- > > Don't stop where the ink does. > > Shawn > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >