Problems? Is your data what you think it is? | |
PerlMonks |
Re^3: Search and Replace in one file based upon contents of anotherby Marshall (Canon) |
on Oct 26, 2016 at 18:08 UTC ( [id://1174804]=note: print w/replies, xml ) | Need Help?? |
Based upon your meager information, here is a framework of code (untested).
note: If the CSV file can have commas within a field (like "Smith,Sr", then you will need a module to help with the parsing. A simple split on comma will not work! I of course was not able to test with real data. I'm sure I've made some error or some detail is being missed, but this is the general idea. Update: I got a msg with a question about why is there a "chomp $line;" in the second while loop and not in the first while loop? I'll put the answer here as others may have the same question... The purpose of chomp() is to remove the line ending, represented as "\n" in Perl. If there was no chomp() in the second while loop, then the last element of @tokens would have the line ending included in that last element after the split on ','. In the first while loop, split ' ',$line; does NOT mean split on the "space" character. This is a special case coded into Perl and is translated into: split on any sequence of whitespace characters, (space,tab,form feed,end of line). So in the first while loop, the split ' ',$line; removes the line ending because it is included in the set of things to split upon. A chomp() before that split would not hurt, but it is not necessary. The difference between split /\s+/, $line; and split ' ', $line; is that in the second version, any whitespace at the front of the line is removed while in the first version, leading whitespace would cause the first element of @tokens to be a null field. Easier demo'ed than further attempts at english explanations: I also added "my" to file handle open statements.
In Section
Seekers of Perl Wisdom
|
|