How do I read a file into an array, but correctly handle duplicates?
I have a file consisting of two columns, a name and a number. Eventually the names will repeat, with a number that may or may not be different.
Rita,13
Sue,11
Bob,01
Too,05
Rita,13
Sue,07
Bob,02
Too,05
I need to read these lines into an array which is not a problem, but repopulate them in a way so that any duplicate name has its value pushed to correct line, which is trickier. For me, at least.
So above should create something like
Rita,13,13
Sue,11,07
Bob,01,02
Too,05,05
There are about 3000 names, and about 600,000 lines to process. (The idea is to highlight which names are stable and which have changing values).
Speed does not matter too much. This will be run about once a week.
Because each line will end up with multiple entries, and the 2nd entry from each line does not matter too much (I need only to read it and populate it to new list), I am thinking I do not need to use a hash here, and I should just iterate through the input file with some form of if exists (or not). Am I right or would a hash be beneficial?
I am using Strawberry Perl V5.32.1 on Windows.
EDIT - thanks for samples, all worked great.
Due to a change in the output, the input file now has extra columns which must remain.
So now it looks like
12,Rita,1,4,13
2,Sue,0,1,11
5,Bob,12,5,01
7,Too,1,4,05
12,Rita,1,4,13
2,Sue,0,1,07
5,Bob,12,5,02
7,Too,1,4,05
and output would be similar, in that only last column will change
12,Rita,1,4,13,13
2,Sue,0,1,11,07
5,Bob,12,5,01,02
7,Too,1,4,05,05
The extra columns will not change but must be there. Does it still make sense to use an array and pluck the 2nd and 5th column to create it, or change the delimiter for first 4 columns so that first column becomes the unique key? That feels dirty but would work...