Think about Loose Coupling | |
PerlMonks |
Re: Multiple File handling and merging records from 2 filesby kcott (Archbishop) |
on Aug 11, 2017 at 06:18 UTC ( [id://1197233]=note: print w/replies, xml ) | Need Help?? |
G'day kris1511, "Is this the right way to read file into nested hash ?" Yes, that's a perfectly legitimate and acceptable way to represent that data. "Not able to wrap my head around the problem" Whenever you have a problem involving CSV (including tab-, pipe-, other-separated data) reach for Text::CSV in the first instance. It will generally do what you want and it's already solved most of the problem cases you're likely to encounter with this type of data. It's also well documented. If you also have Text::CSV_XS installed, it will run faster. Here's the guts of the code to do what you want.
Output:
I've used Inline::Files for demonstaration purposes. You'll probably want to open disk files; replacing "\*CSV1" with something like "$csv_fh1" (ditto for "\*CSV2"). You may want to look at "perldata: Slices" if you don't recognise the syntax within the while loops. If you have a recent version of Perl, look at "perlref: Postfix Reference Slicing": I find that syntax is easier to read and less easy to make mistakes with - you may too. See also Data::Dump if you're unfamiliar with that module. — Ken
In Section
Seekers of Perl Wisdom
|
|