Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by Tux (Monsignor)
on Sep 19, 2012 at 07:58 UTC ( #994424=note: print w/ replies, xml ) Need Help??


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

That last loop obviously is wrong. You cannot loop over a file handle. Certainly not over one that has been read to the end. Compare to below example and work out what you want exactly yourself

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}; # NO "my" the +re! } 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->{fullname}} = $row->{branch}; # NO "my" there! } $csv->eol ("\n"); # Now use it for output open my $fh3, ">", "completed.csv" or die "completed.csv: $!"; $csv->print ($fh3, [qw( user date_modified branch )]); foreach my $user (sort keys %File1Map) { $csv->print ($fh3, [ $user, $File1Map{$user}, $File2Map{$user} || +"?" ]); } close $fh3;

Enjoy, Have FUN! H.Merijn


Comment on Re^5: Merging Two CSV files Based on Two Columns
Download Code
Re^6: Merging Two CSV files Based on Two Columns
by TheCanadian (Initiate) on Sep 19, 2012 at 16:17 UTC

    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 :-}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2015-07-01 21:50 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 (22 votes), past polls