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

comment on

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

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


In reply to Re: Merging Two CSV files Based on Two Columns by kcott
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 cooling their heels in the Monastery: (2)
As of 2024-04-26 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found