while () { # remove leading whitespace & newline from end # and split the row on whitespace my @r = chomp && s/^\s+// && split /\s+/; # create a key in lut hash using rgb vals push @{$lut{"$r[1].$r[2].$r[3]"}}, $r[0]; } while (my ($k, $v) = each %lut) { $k =~ s/\./ /g; # replace . in hash key with space my @v = sort @$v; # sort the color brightness array to get # min/max values print OUTFILE "CLASS\n" . " EXPRESSION ([pixel] >= $v[0] AND [pixel] <= $v[$#v]\n" . " STYLE\n" . " COLOR $k\n" . " END\n" . "END\n"; }