in reply to Most efficient record selection method?
Resulting in:use strict; my (%first, %second, %third); while (<DATA>) { my ($first, $second, $third, $fourth) = split ','; print unless exists $first{$first} and exists $second{$second} and + exists $third{$third}; $first{$first}++; $second{$second}++; $third{$third}++; } __DATA__ A1, B1, C1, first record A2, B2, C2, second record A5, B2, C3, third record A4, B1, C3, fourth record A1, B3, C5, fifth record A2, B3, C5, sixth record A3, B1, C2, seventh record A4, B2, C4, eight record A1, B3, C4, nineth record A2, B1, C1, tenth record
And if you dump the hashes you find how many of each of the variants occurred.A1, B1, C1, first record A2, B2, C2, second record A5, B2, C3, third record A4, B1, C3, fourth record A1, B3, C5, fifth record A3, B1, C2, seventh record A4, B2, C4, eight record
In a real world application you would parse the CSV data with the usual Text::CSV of course.
Update: On reading your question again, I see that I did not take into account the fact that you wanted to have all variants shown in the least number of records. However, I do not think this is something you can exactly solve in any reasonable time, it smells "Travelling salesman" to me. But as a gut feeling , I doubt that when working with real world data, there is much to be gained by this optimization, unless you have real strange data.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Most efficient record selection method?
by kyle (Abbot) on Feb 12, 2009 at 20:39 UTC | |
by CountZero (Bishop) on Feb 12, 2009 at 22:59 UTC | |
by ELISHEVA (Prior) on Feb 13, 2009 at 12:16 UTC |