in reply to Re: Allocation of anonymous arrays
in thread Allocation of anonymous arrays
Thanks for reply Athanasius (and to everyone who has replied so promptly)
With reference to your first example, in theory I would have thought perl (or "a Similar Language") could indeed have @foo and @bar refer to the same storage initially. Then as soon as any code wanted to amend one of them, or even define a reference to them, perl could create a copy and start using that.
(Granted this probably wouldn't be a good idea in practice because most arrays defined explicitly will usually be amended, or references to them defined, during the subsequent running of the program.)
Literal array references not initially assigned to a variable seem even more suitable for this "copy on write" treatment, as they are initially and quite likely never amended or pointed to explicitly. But I can see that in principle one could have something like:
my %fred = ( [1, 2, 3] = [1, 1, 0], [3, 4, 2] = [1, 0, 1], [2, 2, 1] = [1, 1, 0], ); my $i = 0; foreach my $ref (keys %fred) ( $ref->[2] = ++$i; )
So I'm still not entirely convinced one way or the other!
As for why my colleague chose this system, it is a test script in which each key array represents a test case in compact form and the value represents outcome flags. (I would have represented these in a rather different way, all in one array for example, but each to their own.)
Regards
John R Ramsden
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Allocation of anonymous arrays
by andal (Hermit) on Feb 07, 2014 at 13:14 UTC | |
|
Re^3: Allocation of anonymous arrays
by AnomalousMonk (Archbishop) on Feb 07, 2014 at 14:22 UTC | |
|
Re^3: Allocation of anonymous arrays
by OwlHoot (Novice) on Feb 07, 2014 at 11:33 UTC | |
by Anonymous Monk on Feb 07, 2014 at 11:49 UTC |