in reply to Merging Two CSV files Based on Two Columns

G'day TheCanadian,

Welcome to the monastery.

It's good to see you've attempted several solutions yourself before seeking help.

Here's a rundown of issues you have with these different methods (some issues occur in more than one method):

Method #1
  • my $datafolder = "F:/Perl_program";

    I suspect that should have a backslash. In single quotes: 'F:\Perl_program'; in double quotes: "F:\\Perl_program".

  • my $dbh = DBI->connect( ... );

    You should check to see if you've actually made the connection. From DBD::CSV:

    DBI->connect ( ... ) or die "Cannot connect: $DBI::errstr";

    This probably would have pointed you to the $datafolder issue.

    Also take a look at DBI; in particular, $DBI::err, $DBI::errstr, PrintError and RaiseError (all listed in the table of contents).

Method #2
  • open FINAL, "<final2.csv" or die();

    You've used open inconsistently with various degrees of error checking. Also, the 3-argument form is preferable. The first two examples from the open documentation:

    open(my $fh, "<", "input.txt") or die "cannot open < input.txt: $!"; open(my $fh, ">", "output.txt") or die "cannot open > output.txt: $!";

    In case of error, both of these will tell you: what you're trying to open; how you're trying to open it; and, why it didn't work.

  • our %hash1;

    You've used our in this and all subsequent methods. my would have been a better choice. I've mostly retained the our in the code snippets below (to avoid correcting dozens of lines of code) but I do recommend you change them to my.

  • while (<FINAL>) { our ($username, $date_modified) = split; $hash1{$username} = $date_modified; }

    The two variables (that you've declared within the scope of the while loop) go out of scope once you exit the while loop. This will be the cause of some of your "uninitialized value" messages. Compare these two pieces of code:

    $ perl -Mstrict -Mwarnings -le 'for (0..1) { our $x = $_ } print $x' Variable "$x" is not imported at -e line 1. Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $ perl -Mstrict -Mwarnings -le 'our $x; for (0..1) { $x = $_ } print $ +x' 1

    So, if you want to use data you've captured inside a block, declare the variables holding that data outside the block.

  • if (our $username = our $fullname){

    I suspect you were grasping at straws somewhat with this syntax :-)

    Firstly, to compare two strings, the operator is eq (= is an assignment operator) - see perlop. If you declared the variables outside the while loop (as indicated above), this would be the appropriate code to use here:

    if ($username eq $fullname) {
Method #3
  • our $line1; ... our @lines1 = (<FINAL>); ... foreach $line1(@line1){

    You declared @lines1 (with an 's') then used @line1 (without an 's'). As you only use $line1 in that single foreach loop, you can localise it to that loop with my. Were you aware that foreach and for are synonymous - see perlsyn - Foreach Loops. Putting all that together, those three lines can be written as these two lines:

    our @lines1 = (<FINAL>); ... for my $line1 (@line1) {
  • foreach $line2(@lines2){ didn't have the missing 's' problem but otherwise the same comments apply here.

Method #4
  • (our $username, our $date_modified) = split(',',$line1,2);

    You have three lines like this. The syntax for declaring a list of variables with our is:

    our ($username, $date_modified) = ...
  • This method also has many of the same problems addressed above.

-- Ken

Replies are listed 'Best First'.
Re^2: Merging Two CSV files Based on Two Columns
by Anonymous Monk on Sep 08, 2012 at 03:46 UTC

    I suspect that should have a backslash.

    Nope

Re^2: Merging Two CSV files Based on Two Columns
by TheCanadian (Initiate) on Sep 14, 2012 at 14:22 UTC

    Hi: Ken

    This was actually the first reply to my question I viewed. I learned quite a bit about proper syntax from your suggestions and tried some new things. However, my script still did not produce the product I was looking for. My errors on two of the methods dropped to zero but what was produced was an empty CSV file. No worries, I am continuing to modify with the help from others in this forum.

    TheCanadian