milanpwc has asked for the wisdom of the Perl Monks concerning the following question:
I am expecting after the assignment in the second line, the array should consists of four elements #0-3, and "exists" should return true for all of them. However, "exists" only returns true for element #3. Why? Have I missed something?my @array; $array[3] = 2; for my $i (0..5) { say $i . ": " . (exists $array[$i]); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Difference between exists and defined
by hippo (Archbishop) on Apr 16, 2019 at 09:36 UTC | |
From the documentation: WARNING: Calling exists on array values is strongly discouraged. The notion of deleting or checking the existence of Perl array elements is not conceptually coherent, and can lead to surprising behavior. I would not use exists with arrays, only with hashes. YMMV. | [reply] |
|
Re: Difference between exists and defined
by LanX (Saint) on Apr 16, 2019 at 10:48 UTC | |
All used slots point to values (scalar, constant ...), but unused slots don't demand much space or even no space. There are only little use cases where using exists on arrays can be of help. You don't want to meddle with such internals.
Cheers Rolf
| [reply] [d/l] [select] |
|
Re: Difference between exists and defined
by thanos1983 (Parson) on Apr 16, 2019 at 09:37 UTC | |
So why you are expecting to be true on not defined elements (not existing elements)? Is it more clear or still confusing? Also take notice from documentation:
Update: I think I understand why you are confused. You are not assigning to all elements in the array the value 2. Only to one element. See below:
BR / Thanos
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [d/l] [select] |
by Veltro (Hermit) on Apr 16, 2019 at 10:13 UTC | |
Your code example, the way you use Data::Dumper to show the contents of the array allows room for misinterpretation of what is realy happening here. I feel that a better code example is the following:
This prints:
As you can see element 0 DOES exists when it is explicitly set to undefined. Using Data::Dumper prints:
| [reply] [d/l] [select] |
by thanos1983 (Parson) on Apr 16, 2019 at 10:52 UTC | |
Hello Veltro, You are right my example was not clear if I do not add the following part:
The reason that this is happening is explained in the documentation exists:
So if you check the array element with exists and you have manually defined undef to the element then exists will return True. :) Thanks for pointing it out. Hopefully it will avoid confusion for future reference. :) BR / Thanos
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [d/l] [select] |
|
Re: Difference between exists and defined
by dsheroh (Monsignor) on Apr 17, 2019 at 07:20 UTC | |
I am expecting after the assignment in the second line, the array should consists of four elements #0-3Why would you expect that? $array[3] is the only element you've assigned a value to, or even mentioned, prior to the loop, so there's no reason for Perl to have allocated storage space for any other elements.
What you have missed is that Perl arrays are (Also, as previous replies have mentioned, the docs warn against using exists on arrays, so this behavior should be considered implementation-dependent and other versions of the perl binary may potentially behave differently. I kind of doubt that they actually would behave differently in this case, but you still shouldn't rely on it in any code that you care about.) | [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on Apr 17, 2019 at 21:31 UTC | |
Update: The ideas I've expressed in this post are apparently neither entirely correct nor entirely incorrect! Please see the posts of LanX here, haukex here and dsheroh here. $array[3] is the only element you've assigned a value to ... so there's no reason for Perl to have allocated storage space for any other elements. ... Perl arrays are sparse data structures ... you can assign to $array[8675309] without consuming ... memory to store ... unused elements ... I think these statements are incorrect regarding Perl positional (if that's the correct term) arrays. (Perl associative arrays are sparse.) Using Windows Task Manager to graph memory usage in real time (Windoze gotta be good for something) when the following code is executed, one can see that assignment to an array element causes contiguous allocation of enough memory to "grow" the array sufficiently to include the assigned element. The same effect is seen with assignment to array length rather than to any element: $#ra = 100_000_000; It's a question of what to do with the allocated memory. Perl arrays are arrays of scalars, and a scalar is constructed by default in the very well-defined state of un-defined-ness; an "undefined" scalar is a completely specified C/C++ object. So how do you initialize the space for 100,000,000 scalars allocated in the example above? The specific way this question is answered from one CPU/OS/Perl implementation to another is the basis of the ambiguity surrounding the use of exists on allocated but never-accessed array elements. My fuzzy understanding of the Perl guts is that to save time (not space!), array elements in the situation described above are quickly created in a state of quasi-existence: the memory is not left as random garbage, but neither is it a sequence of fully-fledged, default-initialized scalars. Hence the advice regarding use of exists with array elements: Don't Do That!™ Perhaps others more familiar with the details of this question can comment on specifics. Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
by LanX (Saint) on Apr 17, 2019 at 23:35 UTC | |
That is to allow dynamic growth on both ends in a very dynamic way. An array has an internal index for the first and last element and allocates twice as much space as reserve for push or unshift. Basically only the range between the first and last existing element need to be stored, plus mentioned reserve. The existing elements are kind of pointers to scalars which are allocated separately. Allocation of new space is only needed if the reserve elements are filled, since this happens in exponential steps of doubling* it's statistically very efficient. Shrinking the array happens just by adjusting the indices for the first and last element. HTH
Cheers Rolf
updatesee here Shift, Pop, Unshift and Push with Impunity! *) not sure anymore about the doubling, maybe confusing that part with hashes. | [reply] |
by haukex (Archbishop) on Apr 18, 2019 at 07:23 UTC | |
I think these statements are incorrect regarding Perl positional (if that's the correct term) arrays. (Perl associative arrays are sparse.) Just wanted to confirm that you're correct that Perl's arrays are not sparse. I haven't yet found a reference in the official docs that says so explicitly, but I'm sure it's somewhere.
| [reply] [d/l] |
by dsheroh (Monsignor) on Apr 18, 2019 at 08:06 UTC | |
To convince myself, I threw together: Running this on a Debian 8.11 machine with perl 5.20.2, I get the result: The array index 5268 that I used for array1 is a magic number, apparently corresponding to the minimum size that my perl allocates for an array when it's initially declared. If I increase the index to 5269, it shows an additional 132k (all the numbers are in kilobytes) allocated when array1 is assigned to. | [reply] [d/l] [select] |