OK, I knocked this up quick, but it's untested.

You didn't say what should happen if a coord triple is only in one file. And probably needs a lot of safety checks, if these are big files.

#!/usr/bin/env perl # Store X,Y,Z coordinates from one file, along with a number and press +ure. # Find a matching coord in a second file, subtract pressure1 from pres +sure2. # Output with the number from the first file. # Sample data: # 0, 4.38336420e+00, -2.47429684e-01, 6.79128885e+00, 1.51447906e+05 # 1, 4.38336420e+00, -2.78662264e-01, 6.79128885e+00, 1.58727812e+05 # 2, 4.38336420e+00, -2.78662264e-01, 6.69878864e+00, 1.61132406e+05 # 3, 4.38336420e+00, -2.47429684e-01, 6.69878864e+00, 1.54500719e+05 # Assume there are 2 filenames on the command line, in the correct ord +er. use strict; use warnings; our $key_format = '%16.8f'; # Fixed decimals for use in keys our $out_format = '%e'; # scientific notation sub read_it { my $line = shift; chomp($line); my ($n, $x, $y, $z, $p) = split /,\s+/, $line; my $key = sprintf "$key_format,$key_format,$key_format", $x, $y, $ +z; return($n, $x, $y, $z, $key, $p); } our %press; # File 1 while (<>) { my ($n, $x, $y, $z, $key, $p) = read_it($_); $press{$key}{num} = $n; $press{$key}{press} = $p; } continue { # If this is the end of the first file, close it and escape the lo +op if (eof) { # "eof", not "eof()"! close ARGV; last; } } # File 2 while (<>) { my ($n, $x, $y, $z, $key, $p2) = read_it($_); if (exists($press{$key})) { my $pdiff = $p2 - $press{$key}{press}; my $n1 = $press{$key}{n}; print "%d, $out_format, $out_format, $out_format, $out_format\ +n", $n1, $x, $y, $z, $pdiff; } } exit;

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: quick way to add value of a hash with same x, y, z? by QM
in thread quick way to add value of a hash with same x, y, z? by buchi2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.