Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Combining two .csv files

by naturalsciences (Beadle)
on Nov 29, 2010 at 14:32 UTC ( #874252=perlquestion: print w/replies, xml ) Need Help??
naturalsciences has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a task at hand. I have produced some data which is in spreadsheet format. It is some measurements from experiment - there are columns for minimum and maximum values taken from a test site. For example
min max
3 7
4 6
5 8
What I'd like to print ( for a presentation) - would be a table like this.
3 - 7
4 - 6
5 - 8
I have created two .csv files one containing the min and one containing the max values, what would be a perl scirpt look like that would take a "cell1" from one .csv a "cell2" form another and produce a third one "cell1 - cell2"?

Replies are listed 'Best First'.
Re: Combining two .csv files
by Corion (Pope) on Nov 29, 2010 at 14:39 UTC

    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.

      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

        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.
Re: Combining two .csv files
by SuicideJunkie (Vicar) on Nov 29, 2010 at 15:29 UTC

    If you have a spreadsheet to start with, why not read it directly, with, for example Spreadsheet::ParseExcel?

    Other than that, you've got a simple task of "loop{read, read, concatenate, write}"

    Try it and see how it goes.

Re: Combining two .csv files
by sundialsvc4 (Abbot) on Nov 29, 2010 at 16:28 UTC

    It does, indeed, strike me that you should have been able to do this with a formula in Excel.

Re: Combining two .csv files
by fisher (Priest) on Nov 29, 2010 at 14:36 UTC
    Your .csv files contains just one line with values (like 3,4,5) or each value on its own line?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://874252]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-06-25 10:52 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.