in reply to Re: (Ovid) Re: uninitialized value in concatenation
in thread uninitialized value in concatenation

When calling $sth->fetchrow_array in a scalar context, you're going to get back an array ref, possibly containing some other array refs (your result rows). So your test, if ($name eq "") {, should probably look more like, if ($name->[0][0] eq "") {, except that this completely disregards the case where no rows are returned.

You might also factor out all of the common code and elminate unnecessary complexity:

my $name = undef; # store subparent for each parent my %parent_hash = ( 'Father' => 'Mother', 'Mother' => 'Father' ); if (exists $parent_hash{$parent}) { my $sub_parent = $parent_hash{$parent}; for my $col ( $parent, $sub_parent ) { my $sql = "select $col from Roster where User=?"; my $rv = $dbh->selectcol_arrayref( $sql, undef, $user ); if ( @$rv ) { # any rows returned? $name = $rv->[0]; # take first one last; } } } # here, $name will be defined if one of above succeeded # etc. ...

Also, why are you using prepared statements (e.g., prepare() and execute()) without placeholders ('?'). You can just call selectrow_arrayref($sql) (or in your case, selectcol_arrayref($sql) since the results will be a single column) to the same effect.

Update: added placeholders, as per cfreak. What was I thinking?

dmm

If you GIVE a man a fish you feed him for a day
But,
TEACH him to fish and you feed him for a lifetime

Replies are listed 'Best First'.
Re: Re(2): (Ovid): uninitialized value in concatenation
by mnlight (Scribe) on Jan 15, 2002 at 10:35 UTC
    This code is not working because the sql is returning a '' value and the if statement is seeing it as being true. what can I change so it evaluates the '' as no value returned.
    # store subparent for each parent my %parent_hash = ( 'Father' => 'Mother', 'Mother' => 'Father' ); if (exists $parent_hash{$parent}) { my $sub_parent = $parent_hash{$parent}; for my $col ( $parent, $sub_parent ) { my $sql = "select $col from Roster where User=?"; my $rv = $dbh->selectcol_arrayref( $sql, undef, $user); if ( @$rv) { # any rows returned? $name = $rv->[0]; # take first one last; } } }

      "... the sql is returning a '' value and the if statement is seeing it as being true."

      This is not correct.

      As written, the if statement is not testing for truth or falseness, but whether or not any rows were returned.

      To accomplish what you seek, change the if statement to read:

      if ( @$rv && $rv->[0] ) { # any NON-BLANK rows returned?

      dmm

Re: Re(2): (Ovid): uninitialized value in concatenation
by IlyaM (Parson) on Jan 15, 2002 at 00:31 UTC
    When calling $sth->fetchrow_array in a scalar context, you're going to get back an array ref, possibly containing some other array refs (your result rows).

    It is not correct. $sth->fetchrow_array in a scalar context returns the value of the first field.

    --
    Ilya Martynov (http://martynov.org/)

Re: Re(2): (Ovid): uninitialized value in concatenation
by mnlight (Scribe) on Jan 15, 2002 at 02:29 UTC
    In this
    my $name = undef; # store subparent for each parent my %parent_hash = ( 'Father' => 'Mother', 'Mother' => 'Father' ); if (exists $parent_hash{$parent}) { my $sub_parent = $parent_hash{$parent}; for my $col ( $parent, $sub_parent ) { my $sql = "select $col from Roster where User=?"; my $rv = $dbh->selectcol_arrayref( $sql, undef, $user ); if ( @$rv ) { # any rows returned? $name = $rv->[0]; # take first one last; } } } # here, $name will be defined if one of above succeeded
    what is the undef do inside the
    my $rv = $dbh->selectcol_arrayref( $sql, undef, $user );

      From the DBI docs:

      selectrow_array @row_ary = $dbh->selectrow_array($statement); @row_ary = $dbh->selectrow_array($statement, \%attr); @row_ary = $dbh->selectrow_array($statement, \%attr, @bind_values);

      where the third form is the one I was using, without specifying any attributes (i.e., undef).

      dmm

      If you GIVE a man a fish you feed him for a day
      But,
      TEACH him to fish and you feed him for a lifetime

      From the DBI docs:

      selectrow_array @row_ary = $dbh->selectrow_array($statement); @row_ary = $dbh->selectrow_array($statement, \%attr); @row_ary = $dbh->selectrow_array($statement, \%attr, @bind_values);

      where the third form is the one I was using, without specifying any attributes (i.e., undef).

      dmm

Re: Re(2): (Ovid): uninitialized value in concatenation
by mnlight (Scribe) on Jan 14, 2002 at 23:33 UTC
    Please explain how I can eliminate some of the complexity.

      To start with, you could eliminate redundancies (as I've tried to demonstrate by refactoring your code), and encapsulate data dependencies in a data structure, to the degree possible (see my %parent_hash in my snippet), rather than if statements.

        I have taken your code and put in my script it works the problem I am having is somehow it does not recognize who the parent is and give send it to $name.
        # store subparent for each parent my %parent_hash = ( 'Father' => 'Mother', 'Mother' => 'Father' ); if (exists $parent_hash{$parent}) { my $sub_parent = $parent_hash{$parent}; for my $col ( $parent, $sub_parent ) { my $sql = "select $col from Roster where User=?"; my $rv = $dbh->selectcol_arrayref( $sql, undef, $user ); if ( @$rv ) { # any rows returned? $name = $rv->[0]; # take first one last; } } } foreach $date ( @volunteer_date ) { $sth = $dbh->prepare ("select distinct Volunteer from Voluntee +r where Volunteer = \'$name\' and Date = \'$date\'"); $sth->execute (); my $chk = $sth->fetchrow_array; #Verify the parent is not signed up. if ($chk eq $name){ print $query->h1({-align=>'center'}, "You are already signed u +p for this date: $date"); } #end if statement #process the information and update the schedule. else{ $sth = $dbh->prepare ("select max(Number) from Volunteer where Date = '$date' and Volunteer = 'TBD'"); $sth->execute (); my $number = $sth->fetchrow_array; $sth = $dbh->prepare ("update Volunteer set Volunteer = '$name' where Date = '$date' and Volunteer = 'TBD' and Number = $number "); $sth->execute (); print $query->h1({-align=>'center'}, "You signed up for this d +ate $date")