Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi: Tux

Thank you for your support. First I want to reiterate what I want from this script. I have two CSV files one that is 544 rows long and has two columns, "username" and "date_modified". The other CSV file has two columns, "fullname" and "branch" but is about 12489 rows long. All the "usernames" from the first CSV file are almost identical to the "fullname" from the second CSV file. So I want a new CSV file that is about 544 rows long but has the following columns; "username", "branch", "date_modified".

Sample File #1 (final2.csv)

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

Sample File #2 (HCDir2.csv)

fullname branch aaron.northway CHIEF FINANCIAL OFFICER BRANC +H aashee.aziz HEALTH PRODUCTS AND FOOD BRAN +CH abbey.klugerman HEALTHY ENVIRONMENTS AND CONS +UMER SAFETY BRANCH abby.hoffman STRATEGIC POLICY BRANCH abderrahim.boussanni CHIEF FINANCIAL OFFICER BRANC +H abdiaziz.nur HEALTHY ENVIRONMENTS AND CONS +UMER SAFETY BRANCH abdool.yassin HEALTH PRODUCTS AND FOOD BRAN +CH abdoulie.manjang N/A abdullah.hassen HEALTH PRODUCTS AND FOOD BRAN +CH abdullah.patel REGIONS AND PROGRAMS BRANCH

From the two sample data I should get a CSV file with only 2 rows and 3 columns.

username branch date_modified aashee.aziz HEALTH PRODUCTS AND FOOD BRANCH 2012-01-27 abdool.yasseen HEALTH PRODUCTS AND FOOD BRANCH 2012-01-31

To tell you the truth I am having a lot of difficulties with the variables $_ and ARGV, I can't seem to read through the script and know what values are kept in these variables. However, I chose your script Tux, using Text::CSV_XS. Yet getting the script to work is another thing. Below is the script I put together from your sample script. It unfortunately is riddled with errors. Would you be so kind and patient as to give me a helping hand with this script.

#!F:\strawberry-perl-5.16.1.1\perl\bin\perl -w use strict; use warnings; #use diagnostics; 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 $row1 = $csv->getline_hr ($fh1)){ my $File1Map{$row1->{username}} = $row1->{date_modified}; } open my $fh2, "<", "HCDir.csv" or die "HCDir.csv: $!"; $csv->column_names ($csv->getline ($fh2)); # Read the header line while (my $row2 = $csv->getline_hr ($fh2)){ my $File2Map{$row2->{fullname}} = $row2->{branch}; } $csv->eol ("\n"); # Now use it for output open my $fh3, ">", "completed.csv" or die "completed.csv: $!"; my $oldvalue my $newvalue while ($fh1){ $oldvalue = $File1Map{"$username"} = "$date_modified"; $newvalue = $File2Map{"$fullname"} = "$branch"; $csv->print ($fh3, [ $username, $newvalue, $oldvalue ]); }

Thank you in advance.

TheCanadian 20120918-1533 :-}


In reply to Re^4: Merging Two CSV files Based on Two Columns by TheCanadian
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (7)
    As of 2014-08-28 04:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (256 votes), past polls