Dear wise Monks

I have made a code to concatene lines from two different files but my code fails to give the expected results.

Could you check ?

I would like to concatene the lines which have the same name of aircraft (second column for the first file, first column for the second file)

In my example, it would be DAL11 which is common to both files

But it seems that chomp does not work and the data are shifted in the wwrong column

The part that is the core of the code is

print OUTFILE "$line_1"; my $meter = $length_1; if (defined $aircraft_id_1){ ################### OPEN THE SECOND INFILE ################### +######### open(INFILE_2,"<${file_2}") or die "Can't open ${file_2} : $!" +; ############################################################## +### while (my $line_2 = <INFILE_2>){ chomp($line_2); my @Elements_2 = split(/;/, $line_2); # warning : in this file, the aircraft_id looked for is in + the first column my $aircraft_id_2 = $Elements_2[0]; # print STDOUT "the aircraft_id in the resemblance_criteri +on_file is : $aircraft_id_2\n"; if ($aircraft_id_1 eq $aircraft_id_2){ print OUTFILE ";$line_2\n"; } } close INFILE_2; }
Here is my whole code, so you can download it ;

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Cwd; my $Current_Dir = getcwd; print STDOUT "the current directory is $Current_Dir\n"; my $file_1 = "$ARGV[0]"; my $file_2 = "$ARGV[1]"; ################### OPEN THE FIRST INFILE ############################ open(INFILE_1,"<$file_1") or die "Can't open $file_1 : $!"; ################################################################# ################### OPEN THE OUTFILE ############################ my $outfile = "outfile_$file_1"; open(OUTFILE,">$outfile") or die "Can't open $outfile : $!"; ################################################################## # print the title of the columns my $titles_line = <INFILE_1>; print OUTFILE "$titles_line"; while (my $line_1 = <INFILE_1>){ chomp($line_1); my @Elements_1 = split(/;/, $line_1); my $aircraft_id_1 = $Elements_1[1]; # print STDOUT "the aircraft_id in the Analysis_slot_list is : $ai +rcraft_id_1\n"; # calculation of the length of $line_1 my $length_1 = @Elements_1; print STDOUT "the length is $length_1\n"; print STDOUT "The Table is @Elements_1\n"; print OUTFILE "$line_1"; my $meter = $length_1; if (defined $aircraft_id_1){ ################### OPEN THE SECOND INFILE ################### +######### open(INFILE_2,"<${file_2}") or die "Can't open ${file_2} : $!" +; ############################################################## +### while (my $line_2 = <INFILE_2>){ chomp($line_2); my @Elements_2 = split(/;/, $line_2); # warning : in this file, the aircraft_id looked for is in + the first column my $aircraft_id_2 = $Elements_2[0]; # print STDOUT "the aircraft_id in the resemblance_criteri +on_file is : $aircraft_id_2\n"; if ($aircraft_id_1 eq $aircraft_id_2){ print OUTFILE ";$line_2\n"; } } close INFILE_2; } else{ while ($meter<40){ print OUTFILE ";"; ++ $meter; } } } close INFILE_1; close OUTFILE;

Here are my two start files (actually parts of it)

First file

Slot_time;Aircraft_Id;EOBT;ETOT;CTOT;ATOT;ETO;CTO;ATO;Last_DLA;EOBT_DL +A;Anticip_DLA_min;Flag_EOBT_DLA;Last_FPL;EOBT_FPL;Anticip_FPL_min;Fla +g_EOBT_FPL;ETOT_First;Delta_ETOTs_min;ATFM_Delay;;;;;;;; 08:40:00;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:40:58;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:41:56;BMA2CW;08:00;08:20;08:33;08:33;08:31;;08:41;07:52:00;08:00;-8 +;;;;;;;;;;;;;;;; 08:42:55;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:43:53;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:44:51;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:45:50;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:46:48;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:47:46;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:48:45;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:49:43;DAL11;08:10;08:30;08:30;08:39;08:47;08:47;08:50;;;;;05:03:00; +08:10;-187;;08:30;0;0;;;;;;;; 08:50:41;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:51:40;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:52:38;;;;;;;;;;;;;;;;;;;;;;;;;;; 08:53:36;ACA879;07:30;07:42;07:42;07:52;08:51;08:51;08:53;;;;;05:06:00 +;07:30;-144;;07:42;0;0;;;;;;;;

Here is the second file

COA45;COA44;COA;B762;KEWR;LIMC;22:05;1325;05:20;320;;; COA57;COA56;COA;B772;KEWR;LFPG;22:30;1350;04:37;277;;; COA67;COA66;COA;B752;KCLE;EGKK;23:35;1415;06:08;368 COA79;COA78;COA;B762;KEWR;LSZH;23:05;1385;05:48;348 DAL11;DAL12;DAL;B772;KATL;EGKK;22:05;1325;05:39;339 DAL117;DAL116;DAL;B763;KATL;EDDS;22:15;1335;06:59;419 DAL119;DAL118;DAL;B763;KJFK;LFPG;23:05;1385;05:31;331 DAL125;DAL124;DAL;B763;KATL;EBBR;22:05;1325;06:14;374 DAL133;DAL132;DAL;B763;KJFK;LGAV;21:35;1295;06:28;388 DAL141;DAL140;DAL;B763;KJFK;EBBR;23:50;1430;06:16;376 DAL149;DAL148;DAL;B763;KJFK;LIRF;21:35;1295;05:05;305 DAL149;DAL150;DAL;B763;KJFK;LIPZ;22:35;1355;05:58;358 DAL151;DAL150;DAL;B763;KJFK;LIPZ;22:35;1355;05:58;358

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