Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How put results into tables

Status
Not open for further replies.

JackTheRussel

Programmer
Joined
Aug 22, 2006
Messages
110
Location
FI
Hi.

I have tiny problem.

I use 2 different sql-queries in my program.

Example
Code:
If ($setup == 1){

$sql ="SELECT MIN(value), MAX(value), AVG(value) from ... LIMIT 5;
Code:
elsif (setup==0){

$sql="SELECT quota, numbers, MIN(value), MAX(value), AVG(value) from ... LIMIT 5;

And if run this query (setup==1) in mysql-promt I get this kind results
Code:
MIN  MAX  AVG
 5    9    7
 4    4    4
10    2    6
 3    7    5
 5    5    5


Now I wonder how I get each values into own tables.

I would like put records into tables like this:
Code:
@MINtable[5, 4, 10, 3, 5]
@MAXtable[9, 4, 2,  7, 5]
@AVGtable[7, 4, 6,  5, 5]
Now when I do like this:
Code:
my @table;
my $sth = $db_connection->prepare ($sql);
$sth->execute ();

while (my @foo = $sth->fetchrow_array ())
{
    push (our @table,  @foo );

}

It puts results into table like this:
Code:
$table[0] = 5
$table[1] = 9
$table[2] = 7
etc...
Could someone help me.
 
You can't put an array into an array, you van only put scalar values there.
Such as a reference to an array.

Code:
while (my @foo = $sth->fetchrow_array ())
{
    push (our @table,  \@foo );

}

might work better.
 
In which case you might as well use

Code:
$ary_ref  = $dbh->selectrow_arrayref($statement);

now that we're at it...
 
MacTommy.

If I make reference into table, I still don't understand how I can set my results into different tables ?

Could you show me somekind example ?
 
You can't put an array into an array

Yes you can, push() expects a list:

push ARRAY,LIST

Code:
@d = (1..9);
@c = ('a'..'z');
push @c,@d;
print @c;

but if you need an array of arrays:

Code:
@d = (1..9);
@e = ('a'..'z'); 
@c = ();
push @c,[@d],[@e];

or an array of array references:

Code:
@d = (1..9);
@e = ('a'..'z'); 
@c = ();
push @c,\@d,\@e;





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this might work:

Code:
my @table;
my $sth = $db_connection->prepare ($sql);
$sth->execute ();

while (my @foo = $sth->fetchrow_array ())
{
    push (@table, [ @foo ]);

}

I don't think you want to declare the array with "our" inside the loop. Try declaring it outside the loop if you really need to use "our" on the array.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top