On Nov 4, 2:16 am, christian1...@gmx.net ("Christian Stalp") wrote:
> Hello together,
> I try to write some arrays into arrays using references.
>
> my ($a, $b, $c, @mytemp, $myref, @my_globael_array)
>
> while(<$myfile>)
> {
>    ($a, $b $c ) = getparameter();
>   �...@mytemp = ($a, $b, $c);
>    $myref = \...@mytemp;
>    push(@my_global_array, $myref);
>
> }
>
> But if I dismantle @my_global_array I get only the last entry, the last array.
>
> Where is the problem?

You're pushing a hard ref  to @mytemp into the global
array each time. But \...@mytemp is at the same memory
address during the loop and consists of individual ref's --
\$a,\$b,$\c -- whose underlying values $a,$b,$c change
with  each iteration  of the loop.   So, when the loop ends,
you just  have ref's to the final loop values.

One solution: declare @mytemp within -- rather than outside --
the loop. This allocates new memory for @mytemp during
each loop iteration to prevent overwriting.

     while(<$myfile>)
       {
           ($a, $b $c ) = getparameter();
           my @mytemp = ($a, $b, $c);
           ...


--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to