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). | [reply] |
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? | [reply] |
|
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
| [reply] [d/l] |
|
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;
Brad
| [reply] [d/l] [select] |
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. Pax, NovMonk
| [reply] |
|
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!
| [reply] |
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
| [reply] [d/l] |
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? | [reply] |
|
i will have a go at this suggestion, thanks
| [reply] |
|
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";
mawe | [reply] [d/l] |
|
|
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;
}
| [reply] [d/l] |