Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Combining two .csv files

by naturalsciences (Beadle)
on Nov 29, 2010 at 15:32 UTC ( #874273=note: print w/replies, xml ) Need Help??

in reply to Re: Combining two .csv files
in thread Combining two .csv files

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 :(

Replies are listed 'Best First'.
Re^3: Combining two .csv files
by kcott (Chancellor) on Nov 29, 2010 at 15:52 UTC

    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.
Re^3: Combining two .csv files
by tospo (Hermit) on Nov 29, 2010 at 16:18 UTC
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://874273]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-03-21 01:39 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (263 votes). Check out past polls.