Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: sort on hash / hash / hash doesn't work

by AppleFritter (Vicar)
on Jul 12, 2014 at 17:33 UTC ( [id://1093390]=note: print w/replies, xml ) Need Help??


in reply to sort on hash / hash / hash doesn't work

I'm not entirely sure what sort order you're expecting there. You're not sorting the timestamps; you're also not sorting the data, merely the keys for each piece of data, so that the keys will always appear in lexicographical order. For instance, if for each chunk of data and each timestamp, your hash contained values for e.g. IP, REFERER and URL, you'd always get them in that order.

I take it you'd like to sort by timestamp? Simply add a sort in foreach $date ( keys %{ $data{$cat} }) {, like so (I've also taken the liberty of adding my declarations so the script will compile under use strict, and removing the HTML bits so that the output's easier to read):

#!/usr/bin/perl use feature qw/say/; use warnings; use strict; my %data = ( 1 => { "1405799856" => { IP => "79.223.46.211" }, "1405879428" => { IP => "79.200.95.44" }, "1405702121" => { IP => "87.149.76.154" }, "1407101135" => { IP => "188.138.41.140" }, "1406108233" => { IP => "82.139.192.95" }, "1407162997" => { IP => "87.149.66.39" }, "1405810508" => { IP => "80.187.110.214" }, "1405791762" => { IP => "84.159.197.17" }, "1405870826" => { IP => "2.246.106.240" }, "1406040423" => { IP => "87.154.119.73" }, "1405810525" => { IP => "85.22.101.198" }, "1405620262" => { IP => "87.149.72.59" }, "1405603261" => { IP => "178.12.215.30" }, "1405806556" => { IP => "80.187.102.26" }, } ); foreach my $cat ( keys %data ) { foreach my $date ( sort keys %{ $data{$cat} }) { foreach my $data_type (sort { $a cmp $b } keys %{ $data{$cat}{ +$date} }) { say "$date\t$data{$cat}{$date}{$data_type}"; } } }

EDIT: added a few characters that didn't copy'n'paste, thanks redbull2012.

Replies are listed 'Best First'.
Re^2: sort on hash / hash / hash doesn't work
by hexcoder (Curate) on Jul 12, 2014 at 17:59 UTC
    yes, but better sort numerically than lexicographically:
    foreach my $date ( sort { $a <=> $b } keys %{ $data{$cat} }) { # ^^^^^^^^^^^^^
Re^2: sort on hash / hash / hash doesn't work
by redbull2012 (Sexton) on Jul 13, 2014 at 06:03 UTC

    You missed "){" at the third "foreach"

    Regards

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found