vivekvp has asked for the wisdom of the Perl Monks concerning the following question:

Help, Here is the code:
$sth=$dbh->prepare("select b.req_id,ccode from rtsdet a,rtshead b where b.req_id =a.req_id and date_stamp BETWEEN '2002/01/01' AND '2002/02/28'"); $sth->execute (); # will dump out all fields $data=$sth->dump_results(); #$data=$sth->dump_results(); #@data=$sth->fetchrow_array(); while (@data = $sth->fetchrow_array() ) { print "Row: @data \n"; ($req,$ccode)=split(/ /,@data);
do I have this right?
print " $req $ccode \n"; } $sth->finish; Here is the output: Row: 1784996 CAABBA 2 Row: 1785155 PHAD 2 Row: 1785153 PHAD 2 Row: 1785173 SEABBA 2 Row: 1785175 SEABBA 2 Row: 1785176 SEABBA 2 Row: 1785176 SEABBA
The Problem: It is not printing the split data - but the number 2. I want to separate the two pieces of data - and eventually use them. Any help as to what I am doing wrong? Thanks, V
He who laughs last, doesn't get the joke.

Edit by tye to remove excessive trailing spaces

Replies are listed 'Best First'.
Re: Split and the tears it caused me.
by YuckFoo (Abbot) on Feb 28, 2002 at 22:38 UTC
    The second argument to split should be a scalar value, not an array. An array in a scalar context returns the number of elements in the array, in this case 2. $req then gets assigned 2.

    In short, no need to split, just assign ($req, $ccode) = @data;

    YuckFoo

      Or, if you don't need the array for anything else (and those are the only two values returned/needed), skip the extra variable and do:
      while ( ($req, $ccode) = $sth->fetchrow_array() )

      Impossible Robot
Re: Split and the tears it caused me.
by steves (Curate) on Mar 01, 2002 at 09:28 UTC

    Taking impossiblerobot's suggestion a step further: If what you need comes back in the first two columns of the result set you can get the best performance binding the result set directly to those variables. Put your bind after the execute() call:

    $sth->execute(); $sth->bind_columns(\$req, \$ccode);

    Then change your fetch to look like this:

    while ($sth->fetch()) { print " $req $ccode\n"; }

    For large result sets I've seen a measurable performance improvement binding directly to variables versus using a fetch that gets the results as an array or hash.

Re: Split and the tears it caused me.
by strat (Canon) on Mar 01, 2002 at 15:15 UTC
    Btw: you can propably save many tears if you evaluate returncodes and errormessages, e.g.
    my $sth=$dbh->prepare($statement) or die "Error in prepare: ", $DBI::errstr, ": ($DBI::err)\n"; $sth->execute() or die "Error in prepare:", $DBI::errstr, ": ($DBI::err)\n";

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"