### pushing individual rows of return (DBI) into seperate arrays

by peppiv (Curate)
 on Aug 28, 2003 at 19:07 UTC Need Help??
peppiv has asked for the wisdom of the Perl Monks concerning the following question:

G'Day. Seems I've lost my path to brain. Thought it was #!/usr/bin/brain -Tw

Dilemma ->
I'm querying a data base and I'm getting and expecting three rows of data. I'm trying to get the first row to be put in an array (@x). Then I want the second row in array y. And eventually I want the third row in array z.

```\$sth->execute() or die \$sth->errstr;

my \$row = 0;

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

last if \$row++ >= 3;
if (\$row = 1){
@x = @result;
}
if (\$row = 2){
@y = @result;
}
if (\$row = 3){
@z = @result;
}

}
print qq(@x<br><br>);
print qq(@y<br><br>);
print qq(@z<br><br>);

\$sth->finish();
If I put the print statement inside the while loop, each array prints all three rows. If I print outside the while loop, it prints only the last row for each array.

The complete return looks like this:
@result =
row 1 - data1
row 2 - data2
row 3 - data3

I need to get it to do this:
@x = row 1 - data1
@y = row 2 - data2
@z = row 3 - data3

Any suggestions and/or help would be most appreciated.

peppiv

Replies are listed 'Best First'.
Re: pushing individual rows of return (DBI) into seperate arrays
by Mr. Muskrat (Canon) on Aug 28, 2003 at 19:13 UTC
Your if statements are true for each row because you use = instead of ==.
Mr. Muskrat Thanks for the fresh pair of eyes. Can't believe I missed something so simple. Too much time staring at code.

Thank you sir

peppiv

Hi peppiv,

Like everyone else, at some time or other, I have been bitten by this. I have noticed lately around the monastery some people using the idiom

```if ( 1 == \$row ){ ... }

the reason being that if ( 1 = \$row ){ ... } would yield an error. ( Can't modify constant item in scalar assignment )

My first impression was that the construct was ugly, and counter-intuitive, but I feel my opinion changing. I haven't included it in any code, yet, but who knows what the future will bring? :-)

Does anyone have any thoughts on whether the use of this construct should be encouraged?

thinker

Re: pushing individual rows of return (DBI) into seperate arrays
by tcf22 (Priest) on Aug 28, 2003 at 19:58 UTC
Why not just use an array of arrays.
```\$sth->execute() or die \$sth->errstr;

my @result_arrays;
while (my @result = \$sth->fetchrow_array()) {
push(@result_arrays, \@results);
}

foreach(@results_arrays){
print qq(@{\$_}<br><br>);
}
\$sth->finish();
And then, why not use the built-in tool to do that:
```\$sth->execute; # I always set RaiseError so I don't have to keep sayin
+g "or die..."
my \$result = \$sth->fetchall_arrayref(); # calls ->finish too
for (@\$result) {
print qq{@\$_<br><br>};
}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://287488]
Approved by ybiC
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2018-05-23 11:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (168 votes). Check out past polls.

Notices?