How can I combine 2 files with 4 columns into hash in perl -
i trying load file 4 columns , many lines hash, need combine file 1st field contains alphanumeric data , match dates here sample of 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 like: aokx 495408 l 04/02/13 04/15/13 swcomp blhx 102 l 04/02/13 04/03/13 wildcom (more 1 date exists 04/30/13)
here have far - totally not work - when testing , want print in $key giving me second field. cannot seem work more 2 fields. stuck here.
my %hash; open file1, "<", "out1.txt" or die "$!\n"; while ( <file1> ) { chomp $_; ( $key, $le, $date, $company ) = split ',', $_; $hash{$key} = $le, $date, $company; push @{ $hash{$key} }, $_; } close file1;
i changed formatting [$le, $date, $company] much. next issue cannot figure out how combine data in 2 files once read hash. need able match first field (car number) date in both files. either file have multiple listing of date. if no date exists still gets written out. if multiple dates need match closest dates each other. (ex. 04/01/2013 04/05/2013 04/06/2013 04/30/2013) hope makes sense.
so here have far (very basic trying figure out each step) appreciated learning , need make work...thx
#!/usr/bin/perl # use strict; use warnings; $cnt = 0; open file1, "<", "out1.txt" or die "$!\n"; %hash; while ( <file1> ) { chomp $_; ( $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 verify records read $cnt=0; close file1; open file2, "<", "out2.txt" or die "$!\n"; open outfile, ">", "final.txt" or die "$!\n"; while (<file2>) { ( $rkey, $rle, $rdate, $rcompany ) = split ',', $_; $hash{$rkey} = [$rle, $rdate, $rcompany]; push @{ $hash{$rkey} }, $_; $cnt++; # print outfile "\n"; #to write out once figure out how combine } print "total rcpl records processed: $cnt\n"; #just verify records read close file2; close outfile;
need output 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
when perl sees line
$hash{$key} = $le, $date, $company;
it interprets list assignment list ($le, $date, $company)
list ($hash{$key})
assigns each item first list matching item in second list , throws away items without matching item. want assign array reference containing values hash key this
$hash{$key} = [$le, $date, $company];
Comments
Post a Comment