in reply to Find the row with shortest string for a given input in a csv file.
Or, possibly one more code line if we really want to cache the length in the hash:use strict; use warnings; use feature qw/say/; my %results; while (<DATA>) { 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,
# ... while (<DATA>) { my ($id, $string) = (split /[,\s]+/)[0,1]; my $cur_len = length $string; next if defined $results{$id} and $cur_len > $results{$id}{len}; $results{$id} = { str => $string, len => $cur_len }; } say "$_ $results{$_}{str}" for sort keys %results; #...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Find the row with shortest string for a given input in a csv file.
by QM (Parson) on Jul 29, 2014 at 08:21 UTC | |
by Anonymous Monk on Jul 29, 2014 at 08:32 UTC |