Solved

Array not initialized error in DBI

Posted on 2011-03-18
10
335 Views
Last Modified: 2012-05-11
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
Comment
Question by:sunny82
  • 6
  • 4
10 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35170224
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
 

Author Comment

by:sunny82
ID: 35181940
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35182002
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
 

Author Comment

by:sunny82
ID: 35182013
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
 

Author Comment

by:sunny82
ID: 35182040
Let me run the last command you sent.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Expert Comment

by:wilcoxon
ID: 35182084
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
 

Author Comment

by:sunny82
ID: 35182140
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
 

Author Comment

by:sunny82
ID: 35182227
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
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 35182427
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
 

Author Comment

by:sunny82
ID: 35183692
Great, That solution worked perfectly.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now