The thing to realize about the  @Data + 0; statement in your reply above is that it forces the evaluation of  @Data in scalar context (yielding the number of elements) and then throws away the result!

All of these statements are equivalent:
    my $n = 0 + @Data;
    my $n = scalar @Data;
    my $n = @Data;
In the first two, the scalar operation and the use of the scalar built-in on the right hand side of the assignment are redundant because the scalar assignment itself supplies all the scalar context that is necessary. (The usage
    my $n = scalar @Data;
is sometimes advanced as a best practice for reasons of maintainability: supposedly, it makes crystal clear your intention to get the number of elements of the array.)

Where forcing scalar context through a null scalar operation or the use of scalar can be useful is in a situation in which the overall context is list, such as in the argument list of the print built-in:

c:\@Work\Perl>perl -wMstrict -le "my @Data = qw(foo bar baz); ;; print 'hoo ', @Data, ' ha'; print 'fee ', 0 + @Data, ' fie'; print 'hic ', scalar @Data, ' hoc'; " hoo foobarbaz ha fee 3 fie hic 3 hoc


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: Count elements in array ref by AnomalousMonk
in thread Count elements in array ref by Anonymous Monk

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.