Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

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

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


In reply to Re^6: 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 contemplating the Monastery: (8)
    As of 2015-07-07 08:00 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 (87 votes), past polls