On May 28, 3:26 pm, [EMAIL PROTECTED] (Brian) wrote:
> On May 27, 9:00 pm, [EMAIL PROTECTED] (Brian) wrote:
>
>
>
>
>
> > Hi All-
> >   I am trudging through some DBI, XML, etc.. I had a problem and was
> > baffled by how to get at array elements out of a series of pushed
> > array refs. But, by simplifying the problem, I found that the syntax I
> > used was in error. here is the small sample, already debugged. Hope
> > this helps someone...
>
> > #!/usr/bin/perl
>
> >         my @tRespsA;
>
> >         my @fieldList = ( "one", "two", "three", "four" );
> >         my @r1 = ( 1, 2, 3, 4 );
> >         my @r2 = ( 13, 14, 15, 16 );
> >         my @r3 = ( 23, 24, 25, 26 );
>
> >         push @tRespsA, [EMAIL PROTECTED];
> >         push @tRespsA, [EMAIL PROTECTED];
> >         push @tRespsA, [EMAIL PROTECTED];
>
> >         foreach my $tRowRef ( @tRespsA ) {
> >                 my $tCnt=0;
> >                 foreach my $tFld (@fieldList) {
> >                         #if ( $tRowRef->[ $tCnt] eq "") { next; }
> >                         print $tFld . "='" . $tRowRef->[ $tCnt++ ] . "' \r";
> >                 }
> >         }
>
>  oh yes, more important than all that minutiae... the push did not
> work for me in the working code.

The push worked absolutely fine.  It just didn't do what you wanted it
to.  Learning how to parse your problem should be your first step
toward becoming a better programmer.

> The array was being rewritten.

Then you didn't delcare your variables in the correct scope.  As a
general rule of thumb, declare your variables in the smallest scope
possible.

> I had to use an array copy
>
>   push @tRespsA, [ @r1 ];   ## copy contents to an anonymous array,
> push array ref

Do you understand *why* that was necessary?  Do you understand the
difference between these two pieces of code?

#Sample 1
my @array;
my @BigArray;
for (1..5) {
   @array = get_contents();
   push @BigArray([EMAIL PROTECTED]);
}

#Sample 2;
my @BigArray;
for (1..5) {
   my @array = get_contents();
   push @BigArray([EMAIL PROTECTED]);
}


In the first, you're reusing the same array over and over again.  Each
time, you're overwriting the contents of the array with new values,
but you keep pushing references to THE SAME ARRAY onto @BigArray.

In the second, you constantly create and destroy a brand new array
each time through the for loop.  The array gets created, it is filled
with contents, a reference to that array is created, that reference is
pushed onto @BigArray, and then that array goes out of scope.  The
contents are still accessable via the reference you pushed onto
@BigArray, but via no other means.  Next iteration, a brand new array
is created.

This might make it more clear:

my @BigArray;
my @array1;
for (1..3) {
   my @array2;
   my $ref1 = [EMAIL PROTECTED];
   my $ref2 = [EMAIL PROTECTED];
   push @BigArray, $ref1, $ref2;

   print "$ref1 - $ref2\n";
}
__END__

Output:
ARRAY(0x37c38) - ARRAY(0x37c80)
ARRAY(0x37c38) - ARRAY(0x2537c)
ARRAY(0x37c38) - ARRAY(0x25094)


Do you see now?  By declaring only one instance of @array1, you're
getting the same array every time you take a reference to it, so
within the for loop, you keep changing the contents of the same
array.  @array2, on the other hand, is a completely new and unrelated
array each time through the for loop.

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to