Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^6: Merging Two CSV files Based on Two Columns

by TheCanadian (Initiate)
on Sep 19, 2012 at 16:17 UTC ( #994478=note: print w/ replies, xml ) Need Help??


in reply to Re^5: Merging Two CSV files Based on Two Columns
in thread Merging Two CSV files Based on Two Columns

Hi: Tux

I finally, with your help got the script to work. I discovered that the common column had to have the same column header. Your line below with the || really helped because it turned out there were 203 usernames from final.csv that were not found in the HCDir.csv; this trapped the error instead of displaying a blank CSV file. Lastly, I needed to turn off "use strict" because it produces the following error.

Global symbol "%File1Map" requires explicit package name at completed. +pl line 15 . Global symbol "%File1Map" requires explicit package name at completed. +pl line 28 . Global symbol "%File1Map" requires explicit package name at completed. +pl line 29 . Execution of completed.pl aborted due to compilation errors.

Other than that the script worked great. Below is a copy of my modified script.

#!F:\strawberry-perl-5.16.1.1\perl\bin\perl -w #use strict; use warnings; use Text::CSV_XS my %File1Map; my %File2Map; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, allow_whit +espace => 1 }); open my $fh1, "<", "final.csv" or die "final.csv: $!"; $csv->column_names ($csv->getline ($fh1)); # Read the header line while (my $row = $csv->getline_hr ($fh1)){ $File1Map{$row->{username}} = $row->{date_modified}; } open my $fh2, "<", "HCDir.csv" or die "HCDir.csv: $!"; $csv->column_names ($csv->getline ($fh2)); # Read the header line while (my $row = $csv->getline_hr ($fh2)){ $File2Map{$row->{username}} = $row->{branch}; } $csv->eol ("\n"); # Now use it for output open my $fh3, ">", "completed.csv" or die "completed.csv: $!"; $csv->print ($fh3, [qw( username date_modified branch)]); foreach my $username (sort keys %File1Map) { $csv->print ($fh3, [ $username, $File1Map{$username}, $File2Map{$u +sername} || +"Not Found In HCDir!" ]); } close $fh1; close $fh2; close $fh3;

I want to thank you again, I am really new to PERL and I learn primarily through examples. Without your help I would still be lost. I like PERL and would like to continue using PERL. I also purchased the Fourth Edition of "Programming Perl" by Tom Christiansen, Brian d Foy, and Larry Wall; dated 2012. I gather this is the Bible but it did not help with my specific situation.

Yours truly,

TheCanadian 20120919-1207 :-}


Comment on Re^6: Merging Two CSV files Based on Two Columns
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994478]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2015-07-28 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls