in reply to Re: Re: Re: sorting hash of hashes
in thread sorting hash of hashes

I agree it _would_ make it more readable and I tried to put it in a sub but the sort option (8 to be precise) comes from $query-param('sortby') (CGI.pm) which, as far as I can tell by failed attempts, I cannot pass thusly: (while using sic strict)
foreach my $x (sort sortsub($query) keys %y) {...

Every line has a time on it and I have to sort a number of logs by like times of day. Data is as follows:

log1.log<br> --------<br> Time=00:00:00.001|Request=det|Category=btyjar<br> Time=00:00:00.002|Request=sdf|Category=345<br> Time=00:00:00.003|Request=fdgh|Category=cvn<br> Time=00:00:00.004|Request=cv|Category=ryui<br> log2.log<br> --------<br> Time=00:00:00.001|Request=h5|Category=56yjh<br> Time=00:00:00.002|Request=hjk|Category=dr6<br> Time=00:00:00.003|Request=qw|Category=345<br> Time=00:00:00.004|Request=thgj|Category=234<br>

ok, that's example data. then to collect the data i use:

for my $log (@logs) { if ($log =~ /\.gz$/) { eval "`gunzip $log`"; next if $@; $log = substr $log, 0, -3; } open(LOG,$log) || warn "log not opened\n"; while (<LOG>) { chomp; my @data = split /\|/; for (@data) { my($cat,$value) = split /=/; $logdata{$log}{$.}{lc($cat)} = $value; } } close LOG; }

output is in one of two forms: log by log (so i do a little "1 of 10", "previous", "next" to switch between) or Merged - all the files in one big table. both output formats need to be sortable. the log by log is easy because I am sorting at the line level but the merged table is where I got stuck.

I do have many lines and so I need to find a way to get the sort to iterate through those lines, pulling out the time and comparing it, while still at the log level to find out in which order to sort all of the lines of all of the logs.

cheers for taking the time to look at this.

Replies are listed 'Best First'.
Re: re: readibility
by suaveant (Parson) on Apr 25, 2001 at 21:05 UTC
    Still a little confused... do you want to sort by log, then by other data? If that is the case you need to use two sorts... do an outer loop to sort by log, then an inner loop to sort by your other data using the log from the first sort...
                    - Ant
      I need to sort all of the lines in all of the logs by the time on each line. so I need to sort $logdata->{log1..logN} by getting Perl's sort to compare $logdata->{$a}->{$line1..$lineN}->{'time'} to $logdata->{$b}->{$line1..$lineN}->{'time'}

      If I first sort the logs and then do an inner sort on time then I will still have segregation by log but I have to put it all together so if an event happened at 12:00:43.001 in the 3rd log, it will appear above the event at 13:34:23.843 in the 1st log, but below the event at 04:04:21.891 in the 7th log.

      larryk

        Well... then you have to decide if you want to sort by the minimum time in lines, the maximum time in lines, or the average time in lines... cuz you can't just sort by a bunch of values...

        Wait.. are you trying to sort all of the lines into a big mix of lines? so that individual lines from all the logs come out in order? If that is the case then you can't do it with a simple sort the way you have your hash of hashes set up. What you should do in that case is build a composite key... $log.$line or something similar, so then you would do...

        foreach (sort $logdata->{$a}{time} cmp $logdata->{$b}{time}) {
        See what I'm saying... because otherwise you can only sort to a granularity of the log level, or you can sort the lines in a log, but not both, without a lot more work...
                        - Ant