For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > Problem with storing data in an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with storing data in an array
Romain groleau

2004-04-07, 6:31 pm

Hi,

my sql query is select a,b ....

and the code to store the results in stach is :

my @stach;
while( my @ary=$sth->fetchrow_array()) {
push @stach, [@ary] ;
}

and this doesn't work if I look at @stach there are
only ARRAY(0x81cff60) in the two columns of @stach.

Can you help me plz?

Thank you.

Höstrusk och grå moln - köp en resa till solen på Yahoo! Resor på adressen http://se.docs.yahoo.com/travel/index.html
Jupiterhost.Net

2004-04-07, 7:42 pm



Romain Groleau wrote:

> Hi,


Howdy

>
> my sql query is select a,b ....
>
> and the code to store the results in stach is :
>
> my @stach;
> while( my @ary=$sth->fetchrow_array()) {
> push @stach, [@ary] ;
> }

[@ary] if a reference just like \@ary

you could do the same thing by doing:

for( @{ $dbh->selectall_arrayref($query) } ) {

my ($a_column,$b_column) = @{ $_ };

}

And all that without having to do the prepare, execute, getchrow_array
dance or worry about array names and simplifying the process of
processing the data :)

HTH

Lee.M - JupiterHost.Net

> and this doesn't work if I look at @stach there are
> only ARRAY(0x81cff60) in the two columns of @stach.
>
> Can you help me plz?
>
> Thank you.
>
> Höstrusk och grå moln - köp en resa till solen på Yahoo! Resor på adressen http://se.docs.yahoo.com/travel/index.html
>

Randy W. Sims

2004-04-07, 7:42 pm

On 4/7/2004 5:45 PM, Romain Groleau wrote:

> Hi,
>
> my sql query is select a,b ....
>
> and the code to store the results in stach is :
>
> my @stach;
> while( my @ary=$sth->fetchrow_array()) {
> push @stach, [@ary] ;
> }
>
> and this doesn't work if I look at @stach there are
> only ARRAY(0x81cff60) in the two columns of @stach.


my @stach;
while(my @ary = $sth->fetchrow_array()) {
push @stach, \@ary;
}

use Data::Dumper;
print Dumper(\@stach);


Randy W. Sims

2004-04-07, 7:42 pm

On 4/7/2004 5:50 PM, Randy W. Sims wrote:

> On 4/7/2004 5:45 PM, Romain Groleau wrote:
>
>
>
> my @stach;
> while(my @ary = $sth->fetchrow_array()) {
> push @stach, \@ary;
> }
>
> use Data::Dumper;
> print Dumper(\@stach);


Oops, sent that too soon.

In perl, a multidimensional array is an array of arrays, i.e. one array
holds an array in each slot.

Imagine an array with 3 slots. (where a slot is represented by [])

@a=
[]
[]
[]

Now imagine that each slot holds a reference to an array, so it looks like:

@a =
[] -> [][][]
[] -> [][][]
[] -> [][][]

To access an element, you have to access the row which returns a
reference to the array of columns

$ra = $a[1]; # access row index 1 and store reference to array of columns

Then to get the item in a specific column, you dereference the array:

$ra->[1]; # dereference and access element 1

To do it all in one step:

$a[1]->[1];

or, just:

$a[1][1];

Regards,
Randy.




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com