Perl-Sensitive Sunglasses | |
PerlMonks |
Re: Search string from an input file in a different fileby marto (Cardinal) |
on Mar 20, 2018 at 16:59 UTC ( [id://1211354]=note: print w/replies, xml ) | Need Help?? |
Assuming you have two files, one contains IDs in the first column, the second contains several rows, each starting with the ID, in place of real example data consider the following ids.csv:
interactions.csv:
Something to get you started, this is not perfect, and I'll expand on that later. In order to run the code you'll need the Text::CSV module, I also use List::Util which has been a core module (included with perl) since v5.7.3. You can install modules using the cpan tool using cpan Text::CSV, if you're going to use perl a lot I suggest you use cpanm (which you'll need to install first) to deal with module instillation. The perl code:
Running the code above with the example data provided returns:
What this does. Opens the ids.csv file, parses the file contents with the Text::CSV module and adds the value of the each first column into an array of IDs. Opens and parses the interations.csv file. For each ID we have in the array, loop through the rows in interactions.csv, check if the value in the first column is the same as the value of $ID and add the value of the third column to the @totals array. Print the sum of the @totals array. If you're new to perl check out:
Hopefully this has been some help. Perfect, I doubt it, I threw this together very quickly, enough to get you started, I hope so. Reading the perl and module documentation linked to above will be beneficial to you. Please ensure you take the time to do this before replying with any questions about the code above.
In Section
Seekers of Perl Wisdom
|
|