Hello!

I have one datafile which contains
no, x, y, z, pressure1
and another which has
no, x, y, z, pressure2.

I have to write them to a result file building the difference, if the x/y/z coordinate from booth files are matching, which contains
no, x, y, z, pressure2-pressure1

The no should be from file1.
The problem now it, that the order of the coordinates in file1 and file2 is not the same.

file1: 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 file2: 0, 4.38336420e+00, -2.47429684e-01, 6.79128885e+00, 3.43969625e+05 1, 4.38336420e+00, -2.47429684e-01, 6.69878864e+00, 3.29992562e+05 2, 4.38336420e+00, -2.78662264e-01, 6.69878864e+00, 3.30176656e+05 3, 4.38336420e+00, -2.78662264e-01, 6.79128885e+00, 3.42821781e+05
(only the first lines, the files containing several thousand lines).

In my first try I compare every value with every to find matching x/y/z.

Is there a way to do it more efficient?
And is it possible, to trim the blanks during reading automatically away, wihout the trim sub?
#!/usr/bin/perl -w # Aufruf: test.pl file1_a file2_i fnout use strict; use warnings; no warnings qw/once/; my (@biga,@bigi); my (%all); my ($nr,$x,$y,$z,$p); my ($tmp); my ($fna,$fni,$fnout); my (@allnr); my ($i); my (@header); # Datei 1 (aussen) Datei einlesen { open (my $DAT,'<',$ARGV[0]) || do {print STDERR "***error*** during + opening $ARGV[0]: $!\n";die;}; (@biga) = <$DAT>; close $DAT; } chomp @biga; # Datei 2 (innen) Datei einlesen { open (my $DAT,'<',$ARGV[1]) || do {print STDERR "***error*** during + opening $ARGV[1]: $!\n";die;}; (@bigi) = <$DAT>; close $DAT; } chomp @bigi; # die Filenamen $fna = $ARGV[0]; $fni = $ARGV[1]; $fnout = $ARGV[2]; # in den Hash einsortieren foreach (@biga) { ($nr,$x,$y,$z,$p,undef) = split(/,/,$_); $nr = &trim($nr); $x = &trim($x); $y = &trim($y); $z = &trim($z); $p = &trim($p); $all{$nr}{'x'} = $x; $all{$nr}{'y'} = $y; $all{$nr}{'z'} = $z; $all{$nr}{'pa'} = $p; push (@allnr,$nr); } # innen dazusortieren foreach (@bigi) { ($nr,$x,$y,$z,$p,undef) = split(/,/,$_); $nr = &trim($nr); $x = &trim($x); $y = &trim($y); $z = &trim($z); $p = &trim($p); for $i (0 .. $#allnr) { if (($all{$i}{'x'} == $x) && ($all{$i}{'y'} == $y) && ($all{$i}{ +'z'} == $z)) { $all{$i}{'pi'} = $p; $all{$i}{'dp'} = $all{$i}{'pa'} - $all{$i}{'pi'}; next; } } } # Ausgabe schreiben { open (my $DAT,'>',$fnout) || do {print STDERR "***error*** during o +pening $fnout: $!\n";die;}; for $i (0 .. $#allnr) { print $DAT "$i, $all{$i}{'x'}, $all{$i}{'y'}, $all{$i}{'z'}, $all{$ +i}{'dp'}\n"; } close $DAT; } # ################################################ # TRIM A STRING # http://www.symablog.de/blog/perl-fuhrende-und-nachfolgende-leerzeich +en-aus-einem-string-entfernen-trim/ # ################################################ sub trim { my $str = $_[0]; $str =~ s/^\s+|\s+$//g; return $str; }
Regards, buchi

In reply to 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.