Use of uninitialized value in split ?

L

Lance Hoffmeyer

I keep getting

Use of uninitialized value in split at ./birthday.pl line 33.
Use of uninitialized value in split at ./birthday.pl line 33.

on some of the lines of my WHILE loop. Some of the other
lines of the WHILE loop work fine.


When running the script below. I did a check outside of the
WHILE loop to see if the problem would still exist. It does
not.




#!/usr/bin/perl -w
use Pg;
use DBI;
use Date::Manip;



# Check to see if this happens outside DB loop
($year, $month, $week, $day) = split(/:/,"-0:4:3:2:0:0:0",4);
print "$year $month $week \n";



$dbh = DBI->connect ( "dbi:pg:dbname=foobar", "foobar",
"foobar"); if ($dbh) {
print "connected\n";

my $Command = "SELECT first_name, last_name, birthday FROM gw_contact_person";

my $sth = $dbh->prepare($Command);
my $Result = $sth->execute;

$today = &UnixDate("today","%Y-%m-%d");
$date1 = ParseDate($today);

while (my @row_ary = $sth->fetchrow_array)
{


$date2 = ParseDate($row_ary[2]);
$date2 =~ s/^[0-9]{4}/2004/;
$flag = DateCalc($date1,$date2,1);
# => YY:MM:WK:DD:HH:MM:SS

($year, $month, $week, $day) = split(/:/,$flag,4);


if ($row_ary[2] ne ""){printf "%35s %25s %15s %15s %15s %15s %5s \n",
$row_ary[0], $row_ary[1], $row_ary[2],$date1, $date2, $flag, $year

}



$sth->finish;
$dbh->disconnect();
} else {
print "Cannot connect to Postgres server: $DBI::errstr\n";
print " db connection failed\n";
}
 
M

Matija Papec

X-Ftn-To: Lance Hoffmeyer

Lance Hoffmeyer said:
Use of uninitialized value in split at ./birthday.pl line 33.
Use of uninitialized value in split at ./birthday.pl line 33.

on some of the lines of my WHILE loop. Some of the other
lines of the WHILE loop work fine.


When running the script below. I did a check outside of the
WHILE loop to see if the problem would still exist. It does
not.


#!/usr/bin/perl -w

"use strict" can be also helpful
use Pg;
use DBI;
use Date::Manip;
($year, $month, $week, $day) = split(/:/,$flag,4);

I guess you're splitting undef and thus the warning.
 
T

Tad McClellan

Lance Hoffmeyer said:
I keep getting

Use of uninitialized value in split at ./birthday.pl line 33.
Use of uninitialized value in split at ./birthday.pl line 33.

on some of the lines of my WHILE loop. Some of the other
lines of the WHILE loop work fine.


You get that message when you use the special "undef" value
as if it actually had been assigned a value.

It very often indicates a bug somewhere...

When running the script below. I did a check outside of the
WHILE loop to see if the problem would still exist. It does
not.


I can't imagine what logic you applied to think that that is
relevant. Where the code is does not matter with regard to
using an undef value, the particular value being used is
what matters.

#!/usr/bin/perl -w


use warnings; # better than -w
use strict;


Please be respectful of the time of the hundreds of people here
by asking for machine-help before resorting to asking for human-help.

# Check to see if this happens outside DB loop
($year, $month, $week, $day) = split(/:/,"-0:4:3:2:0:0:0",4);


Check to see if this happens when split is passed a *literal string*.

In a loop or not has nothing to do with this problem.

You get no warning because you are not using undef here.

$dbh = DBI->connect ( "dbi:pg:dbname=foobar", "foobar",
"foobar"); if ($dbh) {
print "connected\n";


(something horrid has happened to the formatting of your code...)

$today = &UnixDate("today","%Y-%m-%d");
^
^
^ why are you using the ampersand there?

$flag = DateCalc($date1,$date2,1);
($year, $month, $week, $day) = split(/:/,$flag,4);


The $flag variable contains undef.

Looks like DateCalc() or its arguments are to blame, but we haven't
been shown the relevant code, so we can't help with debugging it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top