in reply to Re: Find the row with shortest string for a given input in a csv file.
in thread Find the row with shortest string for a given input in a csv file.
Do not use parse (it'll break your script on fields with newlines). Use getline instead!
Use auto_diag
I seriously doubt if all the whitespace should be counted in the length function
use 5.12.2; use warnings; use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, allow_whitesp +ace => 1 }); my %results; while (my $row = $csv->getline (*DATA)) { my $uniqueID = $row->[0]; my $string = $row->[1]; $results{$uniqueID}{len} // 9999 <= length $string and next; $results{$uniqueID} = { len => length $string, row => $row, }; } $csv->eol ("\n"); $csv->print (*STDOUT, $results{$_}{row}) for sort keys %results; __DATA__ A, texttexttext, col3, col4, B, textt, col3, col4, A, text, col3, col4, B, texttex, col3, col4,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Find the row with shortest string for a given input in a csv file.
by AppleFritter (Vicar) on Jul 28, 2014 at 18:34 UTC |