caciqueman has asked for the wisdom of the Perl Monks concerning the following question:

I have a correctly created db file I would like to print out, and need to sort it in decending order numerically on the first field. When I use the following, it works fine:
open(INF,"$Dir/log.txt") or dienice("Can't open logs: $! \n"); @stats = <INF>; close(INF); foreach $i (sort byrate @stats) { chomp($i); ($rate,$team,$comment,$gotdate) = split(/\|/,$i); print "$rate $team $comment\n"; print "<br>"; print "$ip $gotdate\n"; print "<br><br>"; } sub byrate { @a = split(/\|/,$a); @b = split(/\|/,$b); $b[0] <=> $a[0]; }
However, no matter what I try, I cant seem to get the print line to work the way I would like. When I use this:
print "[$rate] [$team] $comment\n"; print "<br>"; print "$ip $gotdate\n"; print "<br><br>"; or this: print "($rate) ($team) $comment\n"; print "<br>"; print "$ip $gotdate\n"; print "<br><br>"; it prints out the list in the correct order, that is numerically with the highest rate on top, BUT after it prints the rows out, it then prints an equal amount of these: [][] [][] [][] or these ()() () () etc. I tried stripping spaces or newlines, tried using a if $i > 0 or if exists ($rate) with no success.
Can anyone help this newkid out ? Thanks.

Replies are listed 'Best First'.
Re: Sort ofa Print Problem
by gav^ (Curate) on Apr 26, 2002 at 02:08 UTC
    my @stats = <DATA>; foreach my $line (sort { $a->[0] <=> $b->[0] } map { chomp; [split /\|/] } @stats) { my ($rate, $team, $comment, $gotdate) = @$line; print "$rate $team $comment\n"; print "<br>"; print "$ip $gotdate\n"; print "<br><br>"; } __DATA__ 10|A|Blah|10/1/02 14|B|Blah p9sdf kljsfd |1/6/01 4647|C|Blah adsg|15/7/02 1235|D|Blah fdfd|10/2/02
    Note the $ip isn't defined anywhere. Is this a typo?

    gav^

      To clarify my response:
      "it only prints the top lines and
      then returns a blank area."
      I mean that it only prints the page header,
      a few lines of html code that is above the
      sample I gave you, but the actual return
      from the amended script is now a blank area.
      Thanks.
Re: Sort ofa Print Problem
by graff (Chancellor) on Apr 26, 2002 at 04:46 UTC
    after it prints the rows out, it then prints an equal amount of these:

    Your sort method is making sure that all the empty lines in your input file are being grouped at the end of the "@stats" array, and your approach to printing with brackets or parens around certain columns applies equally to data and to empty lines.

    As a funny coincidence, the number of lines with actual stats seems equal to the number of blank lines in the file.

    See if it helps to add this line, just before the "chomp", inside the "foreach" loop:

    last if ( $i =~ /^\s*$/); # skip lines that are whitespace only
    (Update: normally, one would use "next" instead of "last" here, but since the lines are sorted with blanks at the end...)
Re: Sort ofa Print Problem
by Fletch (Bishop) on Apr 26, 2002 at 02:11 UTC

    Not a direct answer to your problem, but consider using a Schwartzian transform rather than repeated splits.

    my @sorted = map { $_->[1] } sort { $b->[0] <=> $a->[0] } map { [ (split(/\|/,$_))[0], $_ ] } @stats;