Yaerox has asked for the wisdom of the Perl Monks concerning the following question:

I'm doing a check between a database and a csv-file. I got a list of values from database and I need to check if there are is a matching line in my csv file, i have to delete this line. At the end I got a csv-file with lines not stored in db now. Long story short, I got an if clause:
my $dbval1 = ""; my $dbval2 = ""; my $val1 = ""; my $val2 = ""; if ($dbval1 eq $val1 && $dbval2 eq $val2) { print "$dbval1 = $val1 && $dbval2 = $val2\n"; }
which is printing values which doesn't match. The values can be alphanumeric and the database is using varchar2.

Update: including some values:
35000970 = 35000913 && AB00035515 = 11506 35000970 = 35000914 && AB00035515 = 121475 35000970 = 35000921 && AB00035515 = 58065


Update 2: I have no idea what was wrong, I stopped working on this and fixed another bug, now this works. Sry for no solution.

Replies are listed 'Best First'.
Re: string compare matches incorrectly
by GrandFather (Saint) on May 22, 2015 at 11:17 UTC

    use Text::CSV or at least chomp your input lines.

    If handling line ends correctly is not the problem then, as others have suggested, show us sample code and data that fails.

    Perl is the programming world's equivalent of English
      Grandfather that was going to be my thought as well -- reading the input from a file would include the line breaks, so they need to be chomped in order to get an accurate comparison.
Re: string compare matches incorrectly
by marto (Cardinal) on May 22, 2015 at 09:32 UTC

    given your script (with values):

    #!/usr/bin/perl use strict; use warnings; my $dbval1 = "35000970"; my $dbval2 = "AB00035515"; my $val1 = "35000913"; my $val2 = "11506"; if ( ( $dbval1 eq $val1 ) && ( $dbval2 eq $val2 ) ){ print "$dbval1 = $val1 && $dbval2 = $val2\n"; }

    it doesn't print anything, as expected:

    D:\>perl derp.pl D:\>

    If you are seeing something different it's likely that you haven't provided a script which reproduces your problem. How do I post a question effectively?.

Re: string compare matches incorrectly
by afoken (Chancellor) on May 22, 2015 at 09:32 UTC

    Your code does not match your output. Please post actual, working code, reduced to the basic problem.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: string compare matches incorrectly
by pme (Monsignor) on May 22, 2015 at 09:28 UTC
    The posted code works correctly. Please post your real code.
    my $dbval1 = "35000970"; my $dbval2 = "AB00035515"; my $val1 = "35000913"; my $val2 = "11506"; if ($dbval1 eq $val1 && $dbval2 eq $val2) { print "equal $dbval1 - $val1 | $dbval2 - $val2\n"; } else { print "not equal $dbval1 - $val1 | $dbval2 - $val2\n"; }
    prints
    not equal 35000970 - 35000913 | AB00035515 - 11506
      I can't post the real code because it's a small part of a huge script which requires productive database data. I'm actual trying to reproduce it.

      I was basically aiming for help if there is an obvious mistake made ...
        I can't post the real code because it's a small part of a huge script which requires productive database data.

        Take an axe and freely hack away anything not related to the compare problem. ;-)

        Try the following steps, in order:

        1. Add use Data::Dumper; to your script. Use the OOP variant to dump variable NAMES and values, as in print DEBUGLOG Data::Dumper->new([$foo,$bar],['foo','bar'])->Useqq(1)->Indent(1)->Dump();. Take a deep breath and look at what Data::Dumper reports. Are the values in the dump equal to the values that you expect? If not, why do your code and your expectations differ?
        2. Does your script (including all self-made modules) use strict, use warnings as it should? If not, add both. Any traces of no warnings, no strict, assignment to $^W in the code? If yes, remove them. Both will make perl complain loudly about some common problems. Fix those problems. no warnings, no strict and $^W should be rarely needed and be limited to very small blocks. $^W outside a block or without local is almost always an error. $^W should be replaced by no warnings qw( ... ), limiting what warnings are disabled. Run the script and look for compare errors. If they are gone, thank for strict and warnings.
        3. Create a new script containing only use strict; use warnings;. Use Data::Dumper to dump some relevant values from the original script. Try to limit the dump to a few cases, preferably a few that compare ok and a few that don't. If you have no better idea, dump only the first ten or so. Copy and paste the dump into the new script. (Remember that Data::Dumper produces executable perl code.) Copy and paste the compare part into the new script. Add minimal code to run over the hardcoded values from Data::Dumper and call the compare code. If that script still compares different things as equal, post the new script here as is.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: string compare matches incorrectly
by Discipulus (Canon) on May 22, 2015 at 09:19 UTC
    You need to use == not = and this is probably catched caught by strict and warnigs
    35000970 == 35000913

    L*
    update: fixed typo, s/catched/caught/ thanks MidLifeXis.
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      This is for my printing only, i use 'eq' ...