in reply to Sorting a logfile by date to print to html

Here's my approach to this problem. I'd use a hash to store this information, which will make it easy to sort on and quick to get back out when it's time to print. Here's an example of how'd I'd do what I think you're asking:
$from = "news.dat.txt"; my %h; open(FILE, "$from") || die "Can't open $from!\n"; while (<FILE>) { chomp; ($title,$url,$site,$date,$description) = split(/~/,$_); # if the date has already been seen, tack this entry on to the hash + value if( $h{$date} ){ $h{$date} .= "|$title~$url~$site~$description"; } # date hasn't been seen, so create a new hash entry else { $h{$date} = "$title~$url~$site~$description"; } } #sort the dates in desc. order foreach $key (sort {$b cmp $a} keys %h) { #do for each item for that date @items = split '\|', $h{$key}; foreach $item (@items) { #split back out the values and print the table my($title,$url,$site,$description) = split /~/,$item; print "<table width=400><tr>"; print "<td><a href=\"$url\"><font color=blue>$title</font></a> +<br><font color=black>$key,$site<br>$description</font></td>"; print "</tr></table><br>"; } }

Replies are listed 'Best First'.
Re: Re: Sorting a logfile by date to print to html
by Anonymous Monk on Oct 10, 2001 at 00:59 UTC
    Thanks a million for all the help. I ended up using Cubicspline's suggestion and it worked perfectly. Now, I just need to disect the code so that I have a better understanding on how hashes work. Thanks.