Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Duplicity Check Help!

by wallisds (Beadle)
on Dec 02, 2010 at 15:27 UTC ( [id://874917]=note: print w/replies, xml ) Need Help??


in reply to Duplicity Check Help!

I would be interested to see the description of your table 'test_add'.

But in the meantime here are some of my initial suggestions:
my $flag_pic; I would define it and turn on warnings (if not already on): use warnings;
And for the defining part change my $flag_pic; to
my $flag_pic = 0;
Also, where you say :
$flag_pic+="1";
I think you should take away the quotes and try this please:
$flag_pic+=1;
Now if I understand your question correctly you don't want to add any duplicate images into your table and you are checking this based on the value of $flag_pic.

I notice you are saying if ($flag_pic ne "1") { #insert }
but you are adding 1 up to 4 times (given the if statements succeed) in your while loop (as shown here so you know what I'm trying to say)
if ( ($row->{image_name_1} ne "") eq ($pics[0] ne "") ) { $flag_pic += "1"; } if ( ($row->{image_name_2} ne "") eq ($pics[1] ne "") ) { $flag_pic += "1"; } #and all the way to image_name_4
... so if you have two cases where the if succeeds, $flag_pic will be set to 2, not 1, and if you have three, $flag_pic = 3, and so forth. So in any case where more than one if-statement succeeds in your while loop $flag_pic will NOT EQUAL 1.

I recommend changing your if-statement at the bottom to
if ($flag_pic == 0) { #insert }

See if any of that helps.
-Dawn

Replies are listed 'Best First'.
Re^2: Duplicity Check Help!
by Anonymous Monk on Dec 02, 2010 at 16:08 UTC
    Well, yes, I am trying to see if any of the images are already in the database, and if I find any match I am setting the value of "$flag_pic" to something, that way I know that a match was found and I don't have to run the INSERT part.

      Right but what I'm trying to say is your insert code will run anytime $flag_pic is NOT EQUAL to '1' and in your while loop you are incrementing $flag_pic by 1 anytime the image name and pic exist.
      This is done four times in the while loop, which is fine, except in your IF (for inserting) you are saying if (flag_pic ne '1'). You need to check if ($flag_pic == 0) because $flag_pic is not equal to 1 if it equals 0 (which is what you are looking for) but it's also not equal to 1 if it equals 2,3 or 4 ... etc. Does that make sense?
      Try updating the code and see if it helps please.

        Here is some test code I wrote based on what you posted. Maybe it will describe better what I was trying to explain in paragraph form... If you try running this I have two modes $test_do_insert = 1 or $test_do_insert = 0 so you can test multiple cases, etc. If this works for you, all you need to do is put your mysql query and insert lines into the code where applicable. Does this solve your problem?

        #!/usr/bin/perl use strict; use warnings; my @pics; my $test_do_insert = 1; #change value to 0 to change variables to test + a case where it should not insert my $image_name_1 = ""; $image_name_1 = "test_image1.jpg" if (!$test_do_insert); $pics[0] = 'test1.jpg'; my $image_name_2 = ""; #$image_name_2 = "test_image2.jpg" if (!$test_do_insert); $pics[1] = "test2.jpg"; my $image_name_3 = ""; #$image_name_3 = "test_image3.jpg" if (!$test_do_insert); $pics[2] = "test3.jpg"; my $image_name_4 = ""; #$image_name_4 = "test_image4.jpg" if (!$test_do_insert); $pics[3] = "test4.jpg"; if (@pics) { my $flag_pic = 0; # while (my $row = $sth->fetchrow_hashref()) { $flag_pic += &check_empty($image_name_1, $pics[0]); print $flag_pic."\n"; $flag_pic += &check_empty($image_name_2, $pics[1]); print $flag_pic."\n"; $flag_pic += &check_empty($image_name_3, $pics[2]); print $flag_pic."\n"; $flag_pic += &check_empty($image_name_4, $pics[3]); print $flag_pic."\n"; # } print "<br>751**$flag_pic**<br>"; # End check if ( $flag_pic == 0 ) { print "doing insert"; } else { print "not doing insert"; } } sub check_empty() { my $img = shift; my $pic = shift; if ( $img ne "" && $pic ne "" ) { return 1; } else { return 0; } }
        It makes sense what you said, but the issue is on these lines
        if(($row->{image_name_1} ne "") eq ($pics[0] ne "")){$flag_pic ++=1;} if(($row->{image_name_2} ne "") eq ($pics[1] ne "")){$flag_pic ++=1;} if(($row->{image_name_3} ne "") eq ($pics[2] ne "")){$flag_pic ++=1;} if(($row->{image_name_4} ne "") eq ($pics[3] ne "")){$flag_pic ++=1;}
        It is not considering the equality, even if the images are not a match it is still incrementing $flag_pic+=1.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://874917]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found