user2406898
user2406898

Reputation: 31

How can I combine 2 files with 4 columns into hash in perl

I am trying to load a file with 4 columns and many lines into a hash, I need to combine it with another file by the 1st field which contains alphanumeric data and match to the dates Here is a sample of my data:

file 1:
AOKX 495408, L,  04/02/13, SWCOMP
AOKX 495408, L,  04/20/13, SWCOMP
BLHX    102, L,  04/01/13, WILDCOM
CRDX   7067, L,  04/05/13, TYCO
WW     9030, L,  04/02/13, HALLI

file2:
AOKX 495408, L,  04/15/13, SWCOMP
BLHX    102, L,  04/03/13, WILDCOM
CRDX   7067, L,  04/20/13, TYCO
WW     9030, L,  04/30/13, HALLI
BLHX    102, L,  04/30/13, WILDCOM

output file needs to look like:
AOKX 495408 L  04/02/13 04/15/13 SWCOMP
BLHX    102 L  04/02/13 04/03/13 WILDCOM (more than 1 date exists 04/30/13)

Here is what I have so far - it so totally does not work - when testing and want to print what is in $key it is giving me the second field. I cannot seem to get this to work with more that 2 fields. so I am stuck here.

my %hash;

open FILE1, "<", "out1.txt" or die "$!\n";

while ( <FILE1> ) {
chomp $_;
my ( $key, $le, $date, $company ) = split ',', $_;
$hash{$key} = $le, $date, $company;
    push @{ $hash{$key} }, $_;
}

close FILE1;

I changed my formatting to [$le, $date, $company] thanks so much. My next issue is I cannot figure out how to combine the data in the two files once they are read into the hash. I need to be able to match the first field (car number) to the date in both files. Either file my have multiple listing of the date. If no date exists it still gets written out. If multiple dates I really need to match the closest dates to each other. (ex. 04/01/2013 04/05/2013 then 04/06/2013 04/30/2013) I hope this makes sense.

So here is what I have so far (very basic just trying to figure out each step) any help is much appreciated as I am just learning and really need to make this work...thx

#!/usr/bin/perl
# 
use strict;
use warnings;

my $cnt = 0;
open FILE1, "<", "out1.txt" or die "$!\n";

my %hash;

 while ( <FILE1> ) {
     chomp $_;
     my ( $key, $le, $date, $company ) = split ',', $_;
     $hash{$key} = [$le, $date, $company];
     push @{ $hash{$key} }, $_;
     $cnt++;
#    print "$key $date\n"; 
 }

 print "total pcon records processed: $cnt\n";  #just to verify all records read
 $cnt=0;

 close FILE1;

 open FILE2, "<", "out2.txt" or die "$!\n";
 open OUTFILE, ">", "final.txt" or die "$!\n";

 while (<FILE2>) {
     my ( $rkey, $rle, $rdate, $rcompany ) = split ',', $_;
     $hash{$rkey} = [$rle, $rdate, $rcompany];
     push @{ $hash{$rkey} }, $_;
     $cnt++;
 #   print OUTFILE "\n"; #to write out once figure out how to combine

 }
 print "total rcpl records processed: $cnt\n";  #just to verify all records read

 close FILE2;
 close OUTFILE;

need output to look like:

 AOKX 495408 L  04/02/13 04/15/2013 SWCOMP 
 AOKX 495408 L  04/20/13            SWCOMP
 BLHX    102 L  04/01/13 04/03/2013 WILDCOM
 BLHX    102 L           04/30/2013 WILDCOM

Upvotes: 3

Views: 841

Answers (1)

user1937198
user1937198

Reputation: 5348

When perl sees the line

$hash{$key} = $le, $date, $company;

It interprets this as a list assignment from the list ($le, $date, $company) to the list ($hash{$key}) which assigns each item from the first list to its matching item in the second list and throws away any items without a matching item. What you want to do is assign an array reference containing the values to the hash key like this

$hash{$key} = [$le, $date, $company];

Upvotes: 4

Related Questions