xg has asked for the wisdom of the Perl Monks concerning the following question:

First of all I'm new to perl and have just started reading "Teach Yourself Perl in 24Hours".

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

1 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#2
SVO 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
File#3
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
Perl Script:
#!/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
    Why woldn't you just do something like the following (untested, sorry):
    #!perl use strict; use warnings; my @handles; my $i = 0; for (@ARGV) { open $handles[$i++], '<', $_ or die "Failed to open $_: $!\n"; } my $line; $i = 0; while (1) { $line = <$handles[$i]>; defined($line) or last; print $line; } continue { ++$i; $i %= @handles; } close $_ for @handles;

    Hope this helped.
    CombatSquirrel.

    Update: Closed files. Thanks to b10m.
    Entropy is the tendency of everything going to hell.
Re: Merging 3 files
by Anonymous Monk on Dec 18, 2003 at 10:14 UTC
    I have been changing and adding the script without the correct results ... and please let me know what is going wrong
    What are the "correct results" supposed to be?
      The correct result should look like this:
      1 RO -54.68 838.75 2 SC -54.68 725.77 3 TR -54.61 89.65 to the last number in file#1
      The following should be stripped from file number 2 and 3 SVO AIN 0 SLD AIN 1
        First of all, file1 contains 31 lines, while the other two files contain 33 lines. This might be because the files are actually way bigger and you decided not to post more, or there *is* a difference, and you should check wheter you really want to join these files.

        A nice GNU/non-Perlish way would be to do it straight from the command prompt with a few simple commands.
        $ nl file2 > f2; nl file3 > f3; join -o 1.5,2.5 f2 f3 | paste file1 -
        You might even pipe it to sed to get the formatting done a little nicer, but that's up to you :)

        Not a Perl way, but a quick-n-dirty way to get the requested output.
        --
        b10m
      I have posted the correct output.