Hi,

On 09/25/2015 05:46 PM, mt1957 wrote:
Found the following using Array;

 > my Array $a;
 > say $a.elems;
1
 > $a.push(1)
[1]
 > say $a.perl
[1]
 > say $a.elems
1


 > my Array $a .= new;
[]
 > say $a.elems;
0

Seems that an uninitialized Array reports one element but pushing values
on the array will define the properly. In this case it is important to
initialize.

I know that this can be surprising, but it's actually quite sensible.

If you iterate over the Array type object, you get one iteration:

for Array { .say }

so it makes sense that Array.elems returns 1.

if .push wouldn't autovivify a defined array, thinks like

my %h;
%h<key>.push: $new_value;

couldn't work.

But the real advise is it use @-sigiled variables when dealing with arrays; they don't exhibit such interesting corner cases.

Cheers,
Moritz

Reply via email to