my ($first, $sort, $second, $tail) = split(/\s+/, $_); #### m/^(\w+)\s+(\d+)\s+(\w+)(.*)$/ #### 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 () { 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 ( "%s%s%s%s\n", $colour{$colour}, $first, $sort, $second, $tail); } print "$buffer";