>>>>> "Marty" == m keane <[EMAIL PROTECTED]> writes:

Marty> This is what I'm looking for. My point about the
Marty> foreach/fetchall_arrayref was only to indicate my
Marty> limited experience using fetchall_arrayref. That's the
Marty> only programming context in which I've used it. 

Fair enough.  If you are just looping through sequentially, however,
you should just use "fetch" to grab one row at a time.  This avoids
loading the whole result set into memory (although some DBD drivers do
this anyway, much to my surprise and annoyance!).  That is, instead
of doing this:

  my $results_aref = $sth->selectall_arrayref();
  foreach $row (@$results_aref) { ... }

Do something like this instead:

  $sth->execute();
  while (my $row = $sth->fetch()) { ... }
  $sth->finish();

With small enough result sets, this won't matter; the latter is a much
more common idiom for it, at least in my experience.

Marty> I've been looking through the perl documentation on
Marty> references,dereferencing, array & hash references...

The canonical references come with perl:

   perldata -- perl data types
   perlref  -- references
   perllol  -- list of lists (a.k.a. array-of-array-refs)
   perldsc  -- data structure cookbook

That should cover all the intricacies of references in perl.

Marty> The only thing I can't get my mind around is how would I change
Marty> one of the array (fields) elements for the 1st record with this
Marty> array of array-refs structure? 

Marty> I want the reference to stay as is.

Hm.  This is a little confusing, as you say you want it to stay "as
is" but you want to change one of the array fields.  I'm interpreting
this to mean that you want to maintain the structure, but modify a
value within one of the arrays.  Is this correct?

Marty> Because this is an array of array-references the syntax is
Marty> eluding me :-\

Consider:

  my @array = ( 1, 2, 3 );
  my $a_ref = \@array;

Now, to access the first member of our array, you can do any of:

  my $first_element       = $array[0];
  my $first_element_still = ${$a_ref}[0];
  my $first_element_also  = $a_ref->[0];

Note that with an actual array (a variable which has an @ sign in
front of it), you just use brackets to index to a certain value.  With
a reference to an array, you have to dereference it before you can
index into the array itself.  This dereference can be "manual" (using
the ${} syntax) or it can be automatic (the -> operator means
"dereference my left-hand side, and then index as appropriate to
what's on my right-hand side").  This is all covered in detail in
perlref.  

You'll also want to contemplate the [] operators, which create
references to arrays without requiring that the arrays be declared
themselves.  That is, instead of needing two statements to get the
valid $a_ref above, we could have just done:

  my $ref_to_anon_array = [ 1, 2, 3 ];

This is part of the core difference between "lists" and "arrays", as
the terms are used in Perl.

Creating an array-of-arrays (usually just called "list-of-lists" or
"lol"), you simply realize that it really should be called "array of
array-refs".  (This is implied by the limitation of perl arrays and
hashes, where they can only store scalar values -- arrays aren't
scalar, but *references* to arrays are!)

So:

  my @first_array  = ( 'a', 1, 3.14159 );
  my @second_array = ( 'b', 2, 2.71828 );

  my @array_of_array_refs = ( \@first_array, \@second_array );
  my $a_ref_to_array_refs = \@array_of_array_refs;

The value returned by selectall_arrayref will have a form similar to
that last variable.  It is a reference to an array where each value is
a reference to a different array.  So, to get a reference to a single
row, we need to dereference and index once:

  my $first_row_aref = $a_ref_to_array_refs->[0];

But to get to a single value, we need to dereference and index again:

  my $first_value = $first_row_aref->[0];

We can combine these two steps into one by substitution:

  my $first_value_also = ($a_ref_to_array_refs->[0])->[0];

Since the -> operator binds in the way we'd expect, those parentheses
are unnecessary, and we can say:

  my $first_value_just = $a_ref_to_array_refs->[0]->[0];

As a final bit of syntactic sugar, perl doesn't require the "->"
operator between index operations.  So, you could write it like this:

  my $first_value_just = $a_ref_to_array_refs->[0][0];

But I personally prefer to use the -> everywhere, if I use it
anywhere.  On the other hand, if you don't use an initial arrow, it
looks nicer to not use any:

  my $first_value_again = $array_of_array_refs[0][0];

Does this answer your questions?

t.

Reply via email to