use strict; use warnings; $|++; use constant X => 0; use constant Y => 1; use constant Z => 2; # take the first x co-ord in file 1 my @f1_coords = do { my $filename = 'file1'; open( my $fh, "<$filename" ) or die "Cannot open '$filename' for reading: $!\n"; my $line = <$fh>; close $fh; split( ' ', $line ); }; # substract the x value in file 2 from it - repeat for all x values in file 2 my $filename = 'file2'; open( my $fh, "<$filename" ) or die "Cannot open '$filename' for reading: $!\n"; my $outname = 'file3'; open( my $out_fh = ">$outname" ) or die "Cannot open '$outname' for writing: $!\n"; while (my $line = <$fh>) { my @f2_coords = split( ' ', $line ); print $out_fh $f1_coords[X] - $f2_coords[X], $/; } close $out_fh; close $fh;