 <p>Something like this perhaps?</p> <pre><code>use strict; use warnings; use autodie; my @data; for my $file (qw/ file1.txt file2.txt /) { open my $fh, '&lt;', $file; local $/; my $data = &lt;$fh&gt;; my $i = 0; push @{$data[$i++]}, $_ for $data =~ /[0-9.]+/g; } my $divisor = pop @data; for (@data) { my $val = $_-&gt;[0] / $divisor-&gt;[0] - $_-&gt;[1] / $divisor-&gt;[1]; printf "%.10f\n", $val * $val; } </code></pre> <p><strong>output</strong></p> <pre><code>0.0000000000 0.0000004258 0.4258414140 0.0000004929 1.3263326558 0.0227301083 0.0000000248 0.0504327886 0.0000000591 0.2468596095 0.4159147013 0.0000004497 0.4545017058 0.0000005315 1.2770423982 0.0947597910 0.0000001021 0.0140095243 0.0000000159 0.4442444469 </code></pre>