I also noticed that you are reading file2 every time you need to lookup an aircraft ID. This could take a lot of time if your files are very large. Instead, read all of the data from file2 into a hash, and lookup the aircraft ID's as keys in that hash. Then you will have read each file only one time through. The code below is an example of how you can do this.

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Cwd; # display current working directory my $Current_Dir = getcwd; print STDOUT "the current directory is $Current_Dir\n"; # input files to work on my $file_1 = "$ARGV[0]"; my $file_2 = "$ARGV[1]"; my $outfile = "outfile_$file_1"; # open files open INFILE_1, '<', $file_1, or die "Can't open $file_1 : $!"; open INFILE_2, '<', $file_2, or die "Can't open ${file_2} : $!"; open OUTFILE, '>', $outfile, or die "Can't open $outfile : $!"; # print the title of the columns my $titles_line = <INFILE_1>; print OUTFILE $titles_line; # create hash of aircraft specific data from file 2 my %aircraftID; while (my $line_2 = <INFILE_2>){ chomp($line_2); my @Elements_2 = split ';', $line_2; my $aircraft_id_2 = shift @Elements_2; $aircraftID{$aircraft_id_2} = \@Elements_2; } close INFILE_2; # read each line of file1 and look for aircraft while (my $line_1 = <INFILE_1>){ chomp($line_1); my @Elements_1 = split ';', $line_1; my $aircraft_id_1 = $Elements_1[1]; my $length_1 = @Elements_1; # monitor the process print STDOUT "the length is $length_1\n"; print STDOUT "The Table is @Elements_1\n"; # print the current line into the output file print OUTFILE "$line_1"; # if the line contains an aircraft ID, search for its data in file 2 if ($aircraft_id_1 && exists $aircraftID{$aircraft_id_1}) { print OUTFILE join(';',@{$aircraftID{$aircraft_id_1}}), "\n"; } else { print OUTFILE (";" x (40-$length_1)), "\n" } } close INFILE_1; close OUTFILE;

In reply to Re: concatenation of lines from two different files by dogz007
in thread concatenation of lines from two different files by steph_bow

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.