Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Use of uninitialized value in split ?

Reply
Thread Tools

Use of uninitialized value in split ?

 
 
Lance Hoffmeyer
Guest
Posts: n/a
 
      12-23-2004
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 ( "dbig: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:WKD: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";
}


 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      12-23-2004
X-Ftn-To: Lance Hoffmeyer

Lance Hoffmeyer <(E-Mail Removed)> wrote:
>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.



--
Matija
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      12-24-2004
Lance Hoffmeyer <(E-Mail Removed)> wrote:
> 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 ( "dbig: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.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      12-24-2004
Lance Hoffmeyer wrote:

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


> $flag = DateCalc($date1,$date2,1);


warn "DateCalc($date1,$date2,1)==undef for $_" unless defined $flag;

-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
"defined $r_libs->{$name} " triggers warning "Use of uninitialized value" Liang Perl 2 08-11-2004 07:00 AM
Use of uninitialized value in concatenation smartins68 Perl 1 06-09-2004 05:46 AM
Re: Use of uninitialized value in concatenation (.) or string Error Sukhbir Dhillon Perl 1 04-05-2004 02:31 AM
Use of uninitialized value in concatenation (.) at register.pl line 38, <STDIN> line 10. G Kannan Perl 1 10-11-2003 11:58 AM



Advertisments