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.

Thanks AF, I just deletd that post. I should have noted your pointed . Thanks
  • Comment on Re^2: Find the row with shortest string for a given input in a csv file.

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 13:06 UTC

    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) { ...
      Or just use Tie::IxHash to have an ordererd assoziative array :-) instead of tracking it on our own.

      Greetings,
      Janek Schleicher