I have one datafile which contains
no, x, y, z, pressure1
and another which has
no, x, y, z, pressure2.
The no should be from file1.
The problem now it, that the order of the
coordinates in file1 and file2 is not the same.
(only the first lines, the files containing several thousand lines).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
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?Regards, buchi#!/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; }
In reply to quick way to add value of a hash with same x, y, z? by buchi2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |