I have been a bit naughty in assuming that it's OK to use
my ($first, $sort, $second, $tail) = split(/\s+/, $_);
to replace your:
m/^(\w+)\s+(\d+)\s+(\w+)(.*)$/
Because if you have multiple spaces immediately after $second they will be lost - but as you're putting the data into HTML table cells this shouldn't be an issue.

NB: I used split(/\s+/, $_) and not split(' ', $_) because you were matching /^(\w+), which may just have been for efficiency and anchoring, but I don't know that you didn't need to make sure that there was no leading white space on the line in the input file.

I have not used GrandFather's map because I needed to do quite a lot to the elements returned by the sort (the tests on $sort and the sprintf) and it looked like it was going to be messy and possibly difficult trying to fit it all in there.

I like map, but I tend to shy away from using it for more than the most basic usage - I know 95%+ of the people I've ever worked with who have to deal with Perl would not be able to understand how the map worked, but could all unroll a foreach loop if they needed to change the code after I had moved on to my next contract.

use strict; use warnings; my $buffer; my $input = "input.dat"; my %colour = ( 'lt_90' => '#00FF00', 'lt_180' => '#FF6600', 'gt_179' => '#FF0000' ); my @lines; open( INPF, $input ) or die "Can't read '$input': $!\n"; while (<INPF>) { my ($first, $sort, $second, $tail) = split(/\s+/, $_); if ( defined $tail && $sort =~ /^\d+$/ ) { push(@lines, [$first, $sort, $second, $tail]); } else { # die "Unable to match any lines: $!\n"; # Do you really want to die with $! here? # There wasn't an error, just a failed test, # so $! won't have a relevant message in it. # (I get "Bad file descriptor" YMMV on your OS?) # Perhaps you want something like: chomp(); die "line: '$_' does not match input format\n"; } } close (INPF); for my $line ( sort { $a->[1] <=> $b->[1] } @lines ) { my ($first, $sort, $second, $tail) = @{$line}; my $colour = "gt_179"; if ( $sort < 90 ) { $colour = "lt_90"; } elsif ( $sort < 180 ) { $colour = "lt_180"; } $buffer .= sprintf ( "<tr bgcolor='%s'><td>%s</td><td>%s</td><td>%s</td><td>%s</td> +</tr>\n", $colour{$colour}, $first, $sort, $second, $tail); } print "$buffer";

In reply to Re: most efficient buffer sort by serf
in thread most efficient buffer sort by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.