in reply to Test Number of Elements In Array
While @array is suitable in many cases, you should be mindful of the possibility of sparsely populated arrays. Consider the following and what numbers might be considered to be "the number of elements in the array".
use strict; use warnings; use Devel::Peek; my @array; $array[5] = 'data'; print "\@array = " . scalar(@array) . "\n"; Dump(\@array, 10); __END__ @array = 6 SV = RV(0x913534c) at 0x9135340 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9145430 SV = PVAV(0x9136250) at 0x9145430 REFCNT = 2 FLAGS = (PADMY) ARRAY = 0x91522e0 FILL = 5 MAX = 5 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 Elt No. 1 Elt No. 2 Elt No. 3 Elt No. 4 Elt No. 5 SV = PV(0x9133048) at 0x91351f0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x9140928 "data"\0 CUR = 4 LEN = 8
|
|---|