Hello. There is a code that takes data from the database, and, according to the regular schedule, writes or does not write data in the database. The code is worked out separately correctly, but if before it is executed exactly the same sub, with a similar regular, it gives an error. I can not understand from what it arises, from the database data is issued without problems. data in text form contains data of the form '2400000032632'

sub CheckCorrectEanForSelfMake { my $sqlqueryean = "SELECT EAN FROM `eanfromreport` WHERE DESCRIPTION is NULL OR DESCRIPTION in ('NULL','NothingFind','') AND EAN is not NULL"; my $dbquery1 = $db->prepare($sqlqueryean); $dbquery1->execute; my @rowPRODGRPs;#Π—Π°Π΄Π°Π΅ΠΌ имя массиву while (my ($rowPRODGRP) = $dbquery1->fetchrow_array()) { push @rowPRODGRPs, $rowPRODGRP; my $EanNameFromDatabase = $rowPRODGRPs[$i]; if ($EanNameFromDatabase =~ /^28/) { my $dbqueryifEanisStartWiht28 = "update EANFROMREPORT set DESCRIPTION = 'Own PRD' WHERE EAN = '$EanNameFromDatabase'"; my $dbqueryEanisStartWiht28 = $db->prepare($dbqueryifEanisStartWiht28); $dbqueryEanisStartWiht28->execute; print "Thith is Own prd\n "; } else { print "Not Own prd\n "; } $i++ } return 1; } 

Throws an error in the if ($EanNameFromDatabase =~ /^28/) line if ($EanNameFromDatabase =~ /^28/)

 Use of uninitialized value $EanNameFromDatabase in substitution (s///) 
  • Have you watched what the value of $EanNameFromDatabase before this if ? - Regent
  • And is there really a mistake in this line? After all, =~ /^28/ - this is a search in the string by reg. expressions, not a replacement ( m// , not s/// ). - Regent
  • one
    What you are trying to do is done in one request. update EANFROMREPORT set DESCRIPTION = 'Own PRD' WHERE EAN like '28%' and (DESCRIPTION is NULL OR DESCRIPTION in ('NULL','NothingFind','')) - Mike

1 answer 1

Right here:

 push @rowPRODGRPs, $rowPRODGRP; my $EanNameFromDatabase = $rowPRODGRPs[$i]; 

What is $i and why are you sure that the value of this variable is not greater than the value of the last index in the $rowPRODGRPs (if it is defined at all)?

Learn to debug the program. If the usual debugger is too complicated to understand, at least with the help of the Data :: Printer module and the usual print / printf :

 use DDP; # ... push @rowPRODGRPs, $rowPRODGRP; p @rowPRODGRPs; # Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ состояниС массива # Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $i - ΠΈΠΌΠ΅Π½Π½ΠΎ Ρ‚Π°ΠΊ, Π½Π΅ ΠΊΠ°ΠΊ %d: printf "i: '%s', \@rowPRODGRPs length: %d\n", $i, scalar @rowPRODGRPs; # ... 

And never write anything without these lines after a shebang:

 use strict; use warnings; # Π½Ρƒ ΠΈΠ»ΠΈ use Modern::Perl; 
  • The error was that it did not define $ i inside sub. Thank. - Evgeniy A