4 ms·
Of course this example is contrived: my @array; # $#array : number of @array elements say $#array ; # print -1 $array[10]=3; say $#array; # print 10
by wazoox 16y ago
Of course this example is contrived:
my @array;
# $#array : number of @array elements
say $#array ; # print -1
$array[10]=3;
say $#array; # print 10
But it is quite common to populate an array, then delete some elements in it for instance.
- randallsquared 16y agoShouldn't your third line print zero instead of negative one? Maybe negative one is a special result of an uninitialized array in perl? Hm. Anyway, the actual use case of populating an array and then deleting some elements works fine in PHP; I don't think there's any difficulty with expressing what you want -- it's just going to be somewhat less efficient than it could be. In your example, it's not clear to me what indexes zero through nine point to... null, maybe? In PHP, you'd have to be explicit about what values you wanted in zero through nine by using array_fill(), or live with: for($i=0;$i<=10;$i++) { if(!isset($array[$i])) continue; // ... } If you use a for loop, you'd have to check each value to make sure it's defined like that. If you don't care about order in the loop, you could use foreach and avoid the check and a lot of extraneous looping.