• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Array not initialized error in DBI

Hi,

I am getting a strange error  while executing the code below, there is one single query which ends with a ";"  and it does not return any row from the database, hence the array should be empty. What can be the reason for this happening?

==============================
my @sql = grep { defined $_ and not /^\s*$/ } split /;/, $query;

          my @sel_data ;

          foreach my $sql (@sql) {

            if ($sql =~ m{\bselect\b}) {
              print "about to prepare:\n$sql\n";
              my $sel_sth = $sel_dbh->prepare($sql) or die "Can't prepare SQL statement: $DBI::errstr\n";
              $sel_sth->execute() or die "Can't execute SQL statement: $DBI::errstr\n";
              push @sel_data, $sel_sth->fetchrow_array;
              $sel_sth->finish;

            }

            else { print "about to do:\n$sql\n";
                  $sel_dbh->do($sql);

            }
          }

          print "\nThe array is @sel_data\n";

=============================


Use of uninitialized value in join or string at Sanity_Tests.pl (at the print line)
0
sunny82
Asked:
sunny82
  • 6
  • 4
1 Solution
 
wilcoxonCommented:
That's expected if there is no data in the array.  To fix it, you could do

# won't print anything if @sel_data is empty
print "\nThe array is @sel_data\n" if @sel_data;

or

# will print something
print "\nThe array is ", join(' ', @sel_data), "\n";
0
 
sunny82Author Commented:
Using both of them gives me the same error. I did as below --

==============================================
 my $sel_dbh = DBI->connect("$server", '', '', { 'RaiseError' => 1 }) or
          die "Can't connect to database: $DBI::errstr";


          my @sql = grep { defined $_ and not /^\s*$/ } split /;/, $query;

          my @sel_data ;

          foreach my $sql (@sql) {

            if ($sql =~ m{\bselect\b}) {
              print "about to prepare:\n$sql\n";
              my $sel_sth = $sel_dbh->prepare($sql) or die "Can't prepare SQL statement: $DBI::errstr\n";
              $sel_sth->execute() or die "Can't execute SQL statement: $DBI::errstr\n";
              push @sel_data, $sel_sth->fetchrow_array;
#              $sel_sth->finish;

            }

            else { print "about to do:\n$sql\n";
                  $sel_dbh->do($sql);

            }
          }

         # print "\nThe array is @sel_data\n" if @sel_data;
          print "\nThe array is ", join(' ', @sel_data), "\n";
=============================================


The error I am getting is the same at the print array line.

Use of uninitialized value in join or string at Sanity_Tests.pl line 169.
 at Sanity_Tests.pl line 36
        main::__ANON__('Use of uninitialized value in join or string at Sanity_Tests....') called at Sanity_Tests.pl line 169

0
 
wilcoxonCommented:
Ah.  If it is still giving the error, then one of the values in @sel_data is undef.

This should fix it:

print "\nThe array is ", join(' ', grep { defined $_ } @sel_data), "\n";
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sunny82Author Commented:
one more thing Iam observing is this ---

if I do like this--

if (@sel_data) {

            print "\nThe array is not empty";

           }

          else {print "\nThe array is empty\n";}


It gives me array is not empty even though the query does not return any rows.
0
 
sunny82Author Commented:
Let me run the last command you sent.
0
 
wilcoxonCommented:
Based on your last few comments, I'm pretty sure the query is not returning any rows but @sel_data does end up with data - 1 (or possibly more) undef values.  This causes @sel_data to be "true" but causes the error you are reporting.
0
 
sunny82Author Commented:
Ok running the command -- print "\nThe array is ", join(' ', grep { defined $_ } @sel_data), "\n";

I find that the array is not empty today, it did not return any rows on Friday.

But I am getting the same error now on a different line.

Use of uninitialized value in join or string at Sanity_Tests.pl line 259.

line 259 of the code contains this, so it is now failing at the if line --
=============================
 if ("@sel_data" eq "0") {

            print "\n\nTest Passed\n\n";

            ###############Writing Passed/Failed To All Sources Sheet#############################################################
            my $wr_parser  = Spreadsheet::ParseExcel::SaveParser->new();
            my $wr_workbook = $wr_parser->Parse("/v/region/na/appl/qatools/util/data/reports/automation_framework/Sanity_Tests/Sanity_Check_RunBook.xls") or die "could not read the excel: $!";
            my $wr_worksheet = $wr_workbook->{Worksheet}[0];
            my $wr_format1 = $wr_workbook->AddFormat(Fill => [0,11,0]);
            my $wr_format2 = $wr_workbook->AddFormat(Fill => [1,10,0]);
            $wr_worksheet->AddCell($all_row, 4, "Pass", $wr_format1);
            $wr_worksheet->AddCell($all_row, 3, $cob_date2, $wr_format1);
            my $oWriteExcel = $wr_workbook->SaveAs("/v/region/na/appl/qatools/util/data/reports/automation_framework/Sanity_Tests/Sanity_Check_RunBook.xls");
            my $format = $oWriteExcel->add_format();
            $format->set_bg_color('lime');
            $format->set_underline();
            $oWriteExcel->sheets(0)->write_url($all_row, 4, 'external:file://///v/region/na/appl/qatools/util/data/reports/automation_framework/Sanity_Tests/Sanity_Batch_Tests_Config.xls', "Pass", $format);
==========================================
0
 
sunny82Author Commented:
The query returns about 12 rows --- I am only seeing one row in the middle, I think the 6th or 7th row when I am printing @sel_data.

So after adding this --
print "\nThe array is ", join(' ', grep { defined $_ } @sel_data), "\n";


The output is this --
The array is 170797582 425 114
Use of uninitialized value in join or string at Sanity_Tests.pl line 259 where 259 is the line
if ("@sel_data" eq "0") {

.
0
 
wilcoxonCommented:
Okay.  It may be simpler to just do:

@sel_data = grep { defined $_ } @sel_data;
print "\nThe array is @sel_data\n";

I think I was wrong initially and the print should work fine (unless @sel_data contains any undef values).

This way, the if statement should work fine as well.  Alternately, and possibly better, you could rewrite the if as:

if (@sel_data == 1 and $sel_data[0] eq '0') {
0
 
sunny82Author Commented:
Great, That solution worked perfectly.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now