Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Merging Two CSV files Based on Two Columns

by philiprbrenan (Monk)
on Sep 07, 2012 at 22:10 UTC ( [id://992390]=note: print w/replies, xml ) Need Help??


in reply to Merging Two CSV files Based on Two Columns

Assuming your data is valid, please consider:

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); my @f1 = split /\n/, <<'END'; 2010-nhw-es-ea-285, 2012-10-10 aab.external.audit.-.bvr.verif.externes, 2012-02-22 aashee.aziz, 2012-01-27 abbas.rangwala, 2012-01-27 abbie.gardner, 2012-09-10 ab.border-frontiere, 2012-08-11 abdool.yasseen, 2012-01-31 abdullah.aboukarr, 2012-08-10 abdullahi.sheikh, 2012-02-28 END my @f2 = split /\n/, <<'END'; aaron.northway, CHIEF FINANCIAL OFFICER BRANCH aashee.aziz, HEALTH PRODUCTS AND FOOD BRANCH abbey.klugerman, HEALTHY ENVIRONMENTS AND CONSUMER SAFETY BRANCH abby.hoffman, STRATEGIC POLICY BRANCH abderrahim.boussanni, CHIEF FINANCIAL OFFICER BRANCH abdiaziz.nur, HEALTHY ENVIRONMENTS AND CONSUMER SAFETY BRANCH abdool.yassin, HEALTH PRODUCTS AND FOOD BRANCH abdoulie.manjang, N/A abdullah.hassen, HEALTH PRODUCTS AND FOOD BRANCH abdullah.patel, REGIONS AND PROGRAMS BRANCH END my (%f1, %f2); for(@f1) {my ($username, $date) = split /,\s*/; $f1{$username} = $date; } for(@f2) {my ($fullname, $branch) = split /,\s*/; $f2{$fullname} = $branch; } my %names = map {($_, 1)} (keys %f1, keys %f2); say "$_, ", ($f1{$_}//'undefined'), ", ", ($f2{$_}//'undefined') for s +ort keys %names;

Produces:

2010-nhw-es-ea-285, 2012-10-10, undefined aab.external.audit.-.bvr.verif.externes, 2012-02-22, undefined aaron.northway, undefined, CHIEF FINANCIAL OFFICER BRANCH aashee.aziz, 2012-01-27, HEALTH PRODUCTS AND FOOD BRANCH ab.border-frontiere, 2012-08-11, undefined abbas.rangwala, 2012-01-27, undefined abbey.klugerman, undefined, HEALTHY ENVIRONMENTS AND CONSUMER SAFETY B +RANCH abbie.gardner, 2012-09-10, undefined abby.hoffman, undefined, STRATEGIC POLICY BRANCH abderrahim.boussanni, undefined, CHIEF FINANCIAL OFFICER BRANCH abdiaziz.nur, undefined, HEALTHY ENVIRONMENTS AND CONSUMER SAFETY BRAN +CH abdool.yasseen, 2012-01-31, undefined abdool.yassin, undefined, HEALTH PRODUCTS AND FOOD BRANCH abdoulie.manjang, undefined, N/A abdullah.aboukarr, 2012-08-10, undefined abdullah.hassen, undefined, HEALTH PRODUCTS AND FOOD BRANCH abdullah.patel, undefined, REGIONS AND PROGRAMS BRANCH abdullahi.sheikh, 2012-02-28, undefined

Replies are listed 'Best First'.
Re^2: Merging Two CSV files Based on Two Columns
by TheCanadian (Initiate) on Sep 14, 2012 at 13:47 UTC

    Thank you very much for your response. Since you had an entry that showed 5.14 version of PERL, I upgraded to the latest version from 5.12 to 5.16. Thank you for that. I am working on testing a number of responses from this forum. All in all I have not been successful. On a positive note I just found another way that my script does not work. (aka Thomas Edison)

    The results you showed is a true merge however, what I am looking for is to show only the entries that that match in both CSV files.

    Thanks again philiprbrenan.

    TheCanadian

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found