Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Search string from an input file in a different file

by niki24 (Initiate)
on Mar 20, 2018 at 11:20 UTC ( [id://1211301]=perlquestion: print w/replies, xml ) Need Help??

niki24 has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I have a csv file with 627 id's named nodes.csv
I have a second file where interactions of these nodes are present named interactions.csv. the rows indicate the id's while there are around 10 different columns having different score.

1) I want to search the node's.csv file in the interactions.csv file one by one and extract the results seperately for the 627 id's. So that for each id, i get the interactions its showing seperately.

2) Also after getting the results, i would like to add up the scores of the 13th column for each id.
  • Comment on Search string from an input file in a different file

Replies are listed 'Best First'.
Re: Search string from an input file in a different file
by marto (Cardinal) on Mar 20, 2018 at 16:59 UTC

    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:

    1, 2, 3,

    interactions.csv:

    1,10,20,30,40, 1,15,25,35,45, 2,3,5,6,7,8, 3,100,200,300,400 2,10,4,-8,16,32 3,300,400,500,600

    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:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Text::CSV; use List::Util qw 'sum'; my @IDs; my $IDfile = 'ids.csv'; my $Intfile = 'interactions.csv'; my $csv = Text::CSV->new() or die "Can't use CSV: " . Text::CSV->error +_diag(); # Open file containing IDs # our example has id's in the first column my $fh; open( $fh, '<', $IDfile ) or die "Can't open $IDfile: $!"; # build an array of IDs for processing; while ( my $row = $csv->getline( $fh ) ){ push @IDs, $row->[0]; } # close csv $csv->eof or $csv->error_diag(); close $fh; # parse interactions open ( $fh, '<', $Intfile ) or die "Can't open $Intfile: $!"; my $data = $csv->getline_all($fh); $csv->eof or $csv->error_diag(); close $fh; # for each ID in our array foreach my $ID ( @IDs ){ my @total; # loop through interactions data foreach my $row ( @$data ){ # if the fist column is equal to the value of $ID if ( $row->[0] == $ID ){ # add the value of the fourth column to the array push @total, $row->[3]; } } # display totals say "Total for ID $ID: " . sum( @total ) if ( @total ); }

    Running the code above with the example data provided returns:

    Total for ID 1: 65 Total for ID 2: -2 Total for ID 3: 800

    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.

Re: Search string from an input file in a different file
by thanos1983 (Parson) on Mar 20, 2018 at 11:39 UTC

    Hello niki24,

    Welcome to the Monastery. What you have tried from your description and where you have failed? Sample of code, data in and expected output is the minimum that we need to help you.

    Update your question, looking forward to your update.

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Search string from an input file in a different file
by marto (Cardinal) on Mar 20, 2018 at 15:05 UTC

    Welcome, this sounds like something that's fairly easy to accomplish using perl, I second thanos1983, an example of your data would make it much easier to help you. See also How do I post a question effectively?. That data doesn't have to be the actual data you're working with if there's anything confidential, but the format should be the same.

    Update: please don't be put off by the trolls.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1211301]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found