in reply to Find the row with shortest string for a given input in a csv file.

Hmm, I realize that some monks might object to that, but do we really need to use Text::CSV when the whole thing can be done in 4 lines of actual code?
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,
Or, possibly one more code line if we really want to cache the length in the hash:
# ... 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
    ...do we really need to use Text::CSV when the whole thing can be done in 4 lines of actual code?

    I appreciate this attitude. While we should be giving options, and educating the OPs to some extent, I don't see the benefit here of installing a module hierarchy for a problem this small. There are additional problems newbies and old hats both face when installing modules, plus the added burden of dependencies.

    I guess I'm a bit surprised that it took this many responses to get to "here it is in 4 lines of code, without a module".

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      I guess I'm a bit surprised that it took this many responses to get to "here it is in 4 lines of code, without a module".

      FAQ fatigue is real :)