Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Bit of maths between two files

by NovMonk (Chaplain)
on Aug 05, 2004 at 15:26 UTC ( #380290=note: print w/replies, xml ) Need Help??

in reply to Bit of maths between two files

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?

Replies are listed 'Best First'.
Re^2: Bit of maths between two files
by Anonymous Monk on Aug 05, 2004 at 15:34 UTC
    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";
        thanks for that, in addition how could i adjust this so that it sumsthe first x in file1 with first x in file2?
      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: note [id://380290]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2023-10-03 23:06 GMT
Find Nodes?
    Voting Booth?

    No recent polls found