Use of uninitialized value in split ?

Discussion in 'Perl Misc' started by Lance Hoffmeyer, Dec 23, 2004.

  1. 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";
    }
     
    Lance Hoffmeyer, Dec 23, 2004
    #1
    1. Advertising

  2. Lance Hoffmeyer

    Matija Papec Guest

    X-Ftn-To: Lance Hoffmeyer

    Lance Hoffmeyer <> 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
     
    Matija Papec, Dec 23, 2004
    #2
    1. Advertising

  3. Lance Hoffmeyer <> 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 ( "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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 24, 2004
    #3
  4. Lance Hoffmeyer

    Joe Smith Guest

    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
     
    Joe Smith, Dec 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,255
    Eric J. Roode
    Oct 11, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,278
    Joe Smith
    Apr 5, 2004
  3. smartins68
    Replies:
    1
    Views:
    6,021
    Joe Smith
    Jun 9, 2004
  4. Liang
    Replies:
    2
    Views:
    766
    Liang
    Aug 11, 2004
  5. Stanley Xu
    Replies:
    2
    Views:
    637
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page