Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I might use the Text::CSV module to parse and compose CSV, perhapse something like the following:

use strict; use warnings; use Data::Dumper; use IO::String; use Text::CSV; my $file1 = IO::String->new(<<EOF) or die "$!"; username, date_modified 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 EOF my $file2 = IO::String->new(<<EOF) or die "$!"; fullname, branch 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 asdf "asdf" asdf,, EOF my $csv1 = Text::CSV->new({ binary => 1, allow_whitespace => 1, sep_char => ',', }); my %date_modified_by_username; $csv1->column_names( $csv1->getline($file1) ); # get header line while(my $href = $csv1->getline_hr($file1)) { $date_modified_by_username{$href->{username}} = $href->{date_modif +ied}; } my $csv2 = Text::CSV->new({ binary => 1, allow_whitespace => 1, sep_char => ',', eol => "\n", }); $csv2->print(\*STDOUT, [ qw{username branch date_modified} ]); $csv2->column_names( $csv2->getline($file2) ); # get header line while(my $href = $csv2->getline_hr($file2)) { my $date_modified = $date_modified_by_username{$href->{fullname}} || 'unknown'; $csv2->print(\*STDOUT, [ $href->{fullname}, $href->{branch}, $date_modified ]); }

With your sample data this produces:

username,branch,date_modified aaron.northway,"CHIEF FINANCIAL OFFICER BRANCH",unknown aashee.aziz,"HEALTH PRODUCTS AND FOOD BRANCH",2012-01-27 abbey.klugerman,"HEALTHY ENVIRONMENTS AND CONSUMER SAFETY BRANCH",unkn +own abby.hoffman,"STRATEGIC POLICY BRANCH",unknown abderrahim.boussanni,"CHIEF FINANCIAL OFFICER BRANCH",unknown abdiaziz.nur,"HEALTHY ENVIRONMENTS AND CONSUMER SAFETY BRANCH",unknown abdool.yassin,"HEALTH PRODUCTS AND FOOD BRANCH",unknown abdoulie.manjang,N/A,unknown abdullah.hassen,"HEALTH PRODUCTS AND FOOD BRANCH",unknown abdullah.patel,"REGIONS AND PROGRAMS BRANCH",unknown

In reply to Re: Merging Two CSV files Based on Two Columns by ig
in thread Merging Two CSV files Based on Two Columns by TheCanadian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found