use strict; use warnings; use feature qw/say/; my %results; while () { my ($id, $string) = (split /[,\s]+/)[0,1]; next if defined $results{$id} and length $string > length $results{$id}; $results{$id} = $string; } say "$_ $results{$_}" for sort keys %results; __DATA__ A, texttexttext, col3, col4, B, textt, col3, col4, A, text, col3, col4, B, texttex, col3, col4,