You are now talking about the finer points of the DBI module... :-)

The simplest way of doing what you want is this:

my $tbl_ary_ref = $query_handle->fetchall_arrayref;

Here though you will need a bit of background as this does exactly what you
are looking for but with a twist. I'll try to explain...

In perl every value you store no mater if it is a scalar ($x = 1) an array
(@x = [0]) or an hash (%x = { 'key' => 1}) they all are stored in your
computers memory. And when you say my $y = $x; what perl will do is make a
copy of $x and call that $y. Now you are taking twice as much memory which
is not a problem if $x has a value of '1' but if $x contains the collected
works of 19th century poets then this can be quite expensive in terms of
memory usage.
So there is another way to achieve the same instead of saying my $y = $x;
you can say my $y = \$x; what perl does in this case is fill $y with a
reference to $x. This reference tells perl where in the memory $x is stored.
this way you save a lot of memory and still have two variables with
identical values in them. Now I suspect that you are already thinking wait a
moment if $y only points to $x and I change something in $x will this not
influence $y, and you are right. For what we are going to be doing this is
not that important but it is good to know for trouble shooting "strange"
behavior in your future scripts. ;-)
Of course what you can do with scalars you can do with arrays and hash's as
well just as easy.

So what $tbl_ary_ref will contain results as a reference to array which
contains references to arrays for each row so basically [ ["column 1",
"column 2"], ["column 1", "column 2"]]. The only thing is you will need to
access this data...
Remember the comment I made about the @{$result} comment in my previous
email thats how it is done, perl sees only the reference so what we did
there is we told perl that it should treat this $result as an array.

So in your case to get to the actual value of column 1 on row 5 of the
result set you would write something like this:

my $row_ref = ${ $tbl_ary_ref }[4]; # Get to the right row (number 4 as perl
arrays start counting at 0 not at 1)
my $column_value = ${ $row_ref }[0]; # Using ${}[0] notation as @{}[0] just
like @array[0] will result in a warning though it will still work for
compatibility reasons

or of course if you want to be fancy:

my $value ${ ${ $tbl_ary_ref }[4] }[0]; # This will provide the same result
as above but is far less readable and not all that desirable because of it

A last point that you will want to know when you start working with
references is that you can always dereference something (basically make a
copy of what ever the reference is pointing to. So you can for instance do
this:
my @array = [ 'an array' ];
my $array_ref = \@array;

my @copy_array = @{$array_ref};

Doing this means that you can safely make changes to the @copy_array without
having to worry about making a mess of the array that $array_ref is pointing
to. It will how ever cause you to have that array in memory twice, so there
is a price to pay for that.

So you should now be able to retrieve the whole result set as a reference to
an array containing references to arrays. And I hope that
my ramblings explained a bit how you can use that reference to get to the
underlying values. If you combine all of that I think you'll be able to work
out the way to get your program to work the way you want it to.

By the way I would if I where you pick a slightly less complex task to learn
a programming language with next time, this one is though interesting
certainly not an easy one to use as a way to learn the language. :-)

Regards,

Rob

On Fri, Mar 4, 2011 at 3:25 PM, mammoccio <vito.pasc...@gmail.com> wrote:

> Il 04/03/2011 13:26, Rob Coops ha scritto:
>
> First of all, really tnx Rob! I really appreciate u way to teach (even
> to folks with no programming background like me) the logic behind!
>
>
> I know that maybe this is a silly question but how can I transform the
>
> my @G1 = (["alfa" ,  "10"], ["beta" ,  "11"]);
>
> In something that take data from the oracle select statement  ?
>
> I mean I tried something like:
>
> my @G1 = $query_handle->fetchrow_array
>
> But it simply don't works....
>
> I was at this point before and I got no solution because I always see
> example that using something like
>
> while (my @G1 = $query_handle->fetchrow_array)
> { do something
> };
>
> and I didn't found a simply solution to put stuff in a array...
>
> Sorry to bother with so silly question, but really I want to learn how
> to do it!
>
> --
> Vito Pascali
> ICT Security Manager
> IT Senior System Administrator
> vito.pasc...@gmail.com
>
>

Reply via email to