xg has asked for the wisdom of the Perl Monks concerning the following question:
I received a perl script some time ago to merge two files. Now I want to use the same script to merge three files. I have been changing and adding the script without the correct results.
Please have a look at the script with the files it should merge and please let me know what is going wrong.
Here are the files:
File#1
File#21 RO 2 SC 3 TR 4 UT 5 MA 6 TU 7 RB 8 AC 9 ZE 10 DH 11 LM 12 AT 13 NN 14 DR 15 GN 16 PN 17 AH 18 RM 19 AR 20 HD 21 VO 22 BO 23 AM 24 SK 25 TA 26 ZM 27 MM 28 RK 29 RM 30 EM 31 SN
File#3SVO AIN 0 -54.68 SVO AIN 0 -54.64 SVO AIN 0 -54.61 SVO AIN 0 -54.63 SVO AIN 0 -54.59 SVO AIN 0 -54.64 SVO AIN 0 -54.69 SVO AIN 0 -54.59 SVO AIN 0 -54.69 SVO AIN 0 -54.58 SVO AIN 0 -54.70 SVO AIN 0 -54.65 SVO AIN 0 -54.61 SVO AIN 0 -54.66 SVO AIN 0 -54.56 SVO AIN 0 -54.65 SVO AIN 0 -54.65 SVO AIN 0 -54.65 SVO AIN 0 -54.62 SVO AIN 0 -54.68 SVO AIN 0 -54.67 SVO AIN 0 -54.69 SVO AIN 0 -54.61 SVO AIN 0 -54.57 SVO AIN 0 -54.60 SVO AIN 0 -54.61 SVO AIN 0 -54.65 SVO AIN 0 -54.62 SVO AIN 0 -54.61 SVO AIN 0 -54.62 SVO AIN 0 -54.71 SVO AIN 0 -54.58 SVO AIN 0 -54.57
SLD AIN 1 838.75 SLD AIN 1 725.77 SLD AIN 1 89.65 SLD AIN 1 523.51 SLD AIN 1 808.10 SLD AIN 1 223.53 SLD AIN 1 19.73 SLD AIN 1 2.26 SLD AIN 1 210.97 SLD AIN 1 310.07 SLD AIN 1 382.20 SLD AIN 1 146.53 SLD AIN 1 157.95 SLD AIN 1 70.09 SLD AIN 1 487.62 SLD AIN 1 164.14 SLD AIN 1 566.46 SLD AIN 1 159.33 SLD AIN 1 247.01 SLD AIN 1 257.50 SLD AIN 1 191.63 SLD AIN 1 181.16 SLD AIN 1 517.60 SLD AIN 1 358.92 SLD AIN 1 146.42 SLD AIN 1 213.83 SLD AIN 1 104.63 SLD AIN 1 158.29 SLD AIN 1 186.81 SLD AIN 1 775.32 SLD AIN 1 419.85 SLD AIN 1 328.96 SLD AIN 1 107.01
#!/usr/local/bin/perl # File: merge.pl #===================================================================== +====== ## Main block { &merge( $ARGV[0], $ARGV[1], $ARGV[2] ); exit(0); } # end main block... #===================================================================== +====== # #===================================================================== +====== sub parseFile_wNames { my($fileSpec) = shift; $i = 0; open(TXT, "$fileSpec") || error("Can't open file $fileSpec"); while ($line =<TXT>) { chomp($line); $Names_Arr[++$i] = $line; } # while close(TXT); return($i); } # parseFile_wNames #===================================================================== +====== # # parses a file with data on format: # SVO AIN 0 -54.67 # SLD AIN 1 680.09 #===================================================================== +====== sub parseFile_sysl { my($fileSpec) = shift; $i = 0; open(TXT, "$fileSpec") || error("Can't open file $fileSpec"); while ($line =<TXT>) { chomp($line); @fields = split(' ' ,$line); # fields[0] = name # fields[1] = 'AIN' # fields[2] = '1' (number)' # fields[3] = <value> <----- THAT'S THE ONE $Val_Arr[++$i] = $fields[3]; } # while close(TXT); return($i); } # parseFile_sysl #===================================================================== +====== sub parseFile_sysv { my($fileSpec) = shift; $i = 0; open(TXT, "$fileSpec") || error("Can't open file $fileSpec"); while ($line =<TXT>) { chomp($line); @fields = split(' ' ,$line); # fields[0] = name # fields[1] = 'AIN' # fields[2] = '1' (number)' # fields[3] = <value> <----- THAT'S THE ONE $Val_Arr[++$i] = $fields[3]; } # while close(TXT); return($i); } # parseFile_sysv # #===================================================================== +====== sub merge { my ($f1, $f2, $f3) = @_; # my ($man, $sec ) = @_; $lines1 = parseFile_wNames( $f1 ); $lines2 = parseFile_sysl( $f2 ); $lines3 = parseFile_sysv( $f3 ); for $i (1 .. $lines1) { print "$Names_Arr[$i] \t\t $Val_Arr[$i] \t $Val_Arr[$i]\n"; } if ($lines1, $lines2 ne $line3) { print "Warning: Different number of lines in the three files $f1 has $lines1 lines $f2 has $lines2 lines $f3 has $lines3 lines\n"; } } # merge #====================================================== # End Of File #======================================================
update (broquaint): added formatting + <readmore> tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Merging 3 files
by CombatSquirrel (Hermit) on Dec 18, 2003 at 10:34 UTC | |
|
Re: Merging 3 files
by Anonymous Monk on Dec 18, 2003 at 10:14 UTC | |
by xg (Initiate) on Dec 18, 2003 at 10:38 UTC | |
by b10m (Vicar) on Dec 18, 2003 at 12:04 UTC | |
by duff (Parson) on Dec 18, 2003 at 19:20 UTC | |
by xg (Initiate) on Dec 18, 2003 at 11:44 UTC |