Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Combining two .csv files

by Corion (Patriarch)
on Nov 29, 2010 at 14:39 UTC ( [id://874255]=note: print w/replies, xml ) Need Help??


in reply to Combining two .csv files

I would assume that such a script would look like Perl code. Or are you more aiming more for what colour it would be?

This is not a script writing service. It is not hard to write a script that combines information from two files to form a third file. If you show us what code you have already written and where you have problems to extend the functionality, then we can help you more.

Replies are listed 'Best First'.
Re^2: Combining two .csv files
by naturalsciences (Beadle) on Nov 29, 2010 at 15:32 UTC
    The files had tables in them like these. Min. file
    Site1 Site2
    3 4
    5 6
    5 8
    And then a similar looking .max file. I couldn't write a script to combine them to a single file that would contain field that have "min - max" in them. I made a workaround and combined those two .csv files into one. Then I was able to write and use this simple script.
    #!/usr/bin/perl -w open IN, "minmaxin.csv" || die ("did not open infile"); open OUT, ">>minmaxtoprint.csv" || die ("did not open outfile"); while (<IN>) { my @line = split(/,/, $_); print OUT "$line[0]-$line[4],$line[1]-$line[5],$line[2]-$line[6],$line +[3]-$line[7]";} close IN; close OUT;
    (As you can see there were four test sites.) Why I made my first post. Is this reason - It is not easy for me to write a script that combines two input files. I have learned as much that i can make simple scripts to modify one file. But regularily I came to a problem like this. A sample code - to illustrate my stupidity/greenness.
    #!/usr/bin/perl -w open MIN, "min.csv" || die ("did not open infile"); open MAX "max.csv" || die ("did not open infile"); open OUT, ">>minmaxtoprint.csv" || die ("did not open outfile"); while (<MIN>) { my @minline = split(/,/, $_); while (<MAX>){my @maxline = split(/,/, $_);}, print OUT "$minline[0]$l +ine[0] ... etc
    Which will obviously not work. The problem is more genral actually - how to I iterate through elements of one entity (file, array etc) while adding/substracting/interacting with elements corresponding to another? That is a thing I would very much like to learn.

    To learn is the reason I joined this site, I'm right now in my "knowledge" of perl only limited to what stuff I have been able to find online. I have had no teacher whatsoever :(

      You can read the files in tandem:

      while (defined(my $min = <MIN>) && defined(my $max = <MAX>)) { ... }

      Take a look at open. The first two examples there show (the preferred) 3-argument form and also use $! for a more meaningful error message if something goes wrong.

      Add use strict; and use warnings; to the top of your code to get messages about problems that may exist. And, as you indicate you're new to Perl, also adding use diagnostics; will provide more verbose messages.

      -- Ken

        Thank you very much. I actually tried something akin to this quite soon but failed - the part of using defined to get a boolean value for my AND part did not come to my mind. Your short post was quite on spot and has been quite enlightening.
      You could have just continued in Excel (assuming that that is what you used in the first place): You can simply concatenate strings from different cells (even different spreadsheets) with "&".
      In Perl, you already know how to read one file and you came across arrays, so just read the contents of file1 into an array and then file2 into another and then find out how to get the number of elements from an array and how to iterate through all elements of an array in a "for" loop (you know that both arrays are of the same size). Inside that loop you just print the two corresponding elements of the two arrays in a line with the "-" between them.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-16 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found