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

In reply to Re: Test Number of Elements In Array by ig
in thread Test Number of Elements In Array by walkingthecow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.