Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Bit of maths between two files

by Anonymous Monk
on Aug 05, 2004 at 14:55 UTC ( #380276=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I have two files with x,y z co-ordinates!
File1: 25 65 75 12 45 89 File2: 85 25 74 66 78 21
I need to add each of the x,y and z values together on each line. I was going to print all values into a single file and use split but how can i read both files and preform this operation? Thanks,

Replies are listed 'Best First'.
Re: Bit of maths between two files
by ysth (Canon) on Aug 05, 2004 at 16:43 UTC
    I don't think any of your posts so far have made it clear exactly what you are trying to accomplish. Perhaps you could show what you want the new file to look like for your sample File1 and File2?

    Some hints that may or may not apply depending on what you actually are trying to do:

    If you want to produce a File3 where line X depends on line X of File1 and line X of File2, you need to be going through File1 and File2 at the same time, not in separate loops. See the question from a couple weeks ago: Combining Lines from Two Docs into Another

    Make sure to only use "my" variables in their scope; they only last until the end of the innermost enclosing {}. If you try to use them beyond that point, you'll get the global variable of the same name instead. Put "use strict;" and "use warnings;" at the top of your script to be notified when you make this mistake (and others).

Re: Bit of maths between two files
by gri6507 (Deacon) on Aug 05, 2004 at 14:59 UTC
    This sounds like a homework problem. Can you show us some code so we know you at least gave it some thought?
      Gone past my best of homwork problems, just trying to pick up perl.
      $file = shift@ARGV; $file2=shift@ARGV; open (FILE, "<$file"); while (<FILE>) { ( $x, $y, $z) = split (/\s+/, $_); } open (FILE2, "<$file2"); while (<FILE2>) { ( $x1, $y1, $z1) = split (/\s+/, $_); } open (OUT, ">output"); print OUT $x $y $z $x1 $y1 $z1; ###then do maths

        Looking at what you've done so far, you're basically there. Perhaps you're confused about how to output a properly formatted line? The last print statement you have will create an output file with no separation between the numbers (and no newline at the end). There are a number of ways (of course) to get it properly formatted. Some examples:

        print OUT "$a $b $c\n"; printf OUT "%d %d %d\n", $a, $b, $c; print OUT join(' ',$a, $b, $c), "\n";

        The latter two will work better if you are going to do the addition inline, e.g.:

        printf OUT "%d %d %d\n", $x+$x1, $y+$y1, $z+$z1;


Re: Bit of maths between two files
by NovMonk (Chaplain) on Aug 05, 2004 at 15:13 UTC
    What do you want when you get done? A file with the sum of each line from both files, printed 1 to a line? If so, why do the original files need to be combined into one at all? Why not do something like this:

    open first file for input
    open output file for write
    split the line
    add up the line elements
    print the result to output file
    open second file and follow the same steps above, continuing to write to the same output file you still have open

    While I could probably use the homework practice myself, I'm deliberately not doing the code for you-- looks like you can handle that. Just suggesting a different way to think about your problem. Or demonstrating I don't understand it.

    Good Luck.



      sorry should have been clearer, need to add each x element from the 2 file to one another then y and z. Not the sum of each line!
Re: Bit of maths between two files
by insensate (Hermit) on Aug 05, 2004 at 18:41 UTC
    perl -ane"$x{$.}+=$F[0];$y{$.}+=$F[1];$z{$.}+=$F[2];if(eof){close ARGV +;if($j){print\"$x{$_} $y{$_} $z{$_}\n\"for keys%x;}$j++}" File1 File2
Re: Bit of maths between two files
by NovMonk (Chaplain) on Aug 05, 2004 at 15:26 UTC
    Oh-- ok. So you need the sum of all x's, sum of all y's, sum of all z's. Seems like you could read them into 3 arrays while you're splitting them, and the add up the elements of each array. Would that work or am I still missing something?
      i will have a go at this suggestion, thanks

        Try this:
        my @file = @ARGV; my @coords; foreach $f (@file) { open (FILE, "<$f"); while (<FILE>) { my @line = split(/\s+/,$_); for my $i (0..$#line) { $coords[$i] += $line[$i]; } } close FILE; } print "@coords\n";
        have tried the following but no success!
        $file = shift@ARGV; $file2=shift@ARGV; open (FILE, "<$file"); while (<FILE>) { @array = ( ); chomp; ( $x, $y, $z) = split (/\s+/, $_); push @array, $x; $size = @array; } open (FILE2, "<$file2"); while (<FILE2>) { ($x1, $y1, $z1) = split (/\s+/, $_); for ($i=0; $i<$size;$i++) { $sum = $x1+$array[$i]; print $sum; } #print $sum; }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-06-29 07:30 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (96 votes). Check out past polls.