Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Find the row with shortest string for a given input in a csv file.

by AppleFritter (Vicar)
on Jul 28, 2014 at 13:06 UTC ( [id://1095351]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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.

No worries. As I said, crossposting is fine as long as you let people know.

Regarding your original question again, if it's important that the order of lines of the original file be preserved, I think it's actually better to augment the hash to hold line numbers instead of using an array, as otherwise you'd have to grep through all previous results in each step to make sure you've not already seen a given unique ID (essentially making the whole loop O(n^2) rather than O(n) with regard to the number of lines in your file).

The hash-based solution is easily augmented to accomplish this:

my %results = (); my $position = 0; while(...) { ... if(...) { $results{$uniqueID} = { ... 'position' => $position++, ... foreach (sort { $results{$a}->{'position'} <=> $results{$b}->{'positio +n'} } keys %results) { ...
  • Comment on Re^3: Find the row with shortest string for a given input in a csv file.
  • Download Code

Replies are listed 'Best First'.
Re^4: Find the row with shortest string for a given input in a csv file.
by bigj (Monk) on Jul 28, 2014 at 14:22 UTC
    Or just use Tie::IxHash to have an ordererd assoziative array :-) instead of tracking it on our own.

    Greetings,
    Janek Schleicher

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1095351]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-20 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found