my $argCount = $#ARGV+1;

To find the number of elements in an array, use the array in scalar context.

 my $argCount = @ARGV;

To find out how this is different (and why it is the *correct* way) read about the variable $[ by doing a perldoc perlvar

The sample code will illustrate that.

use strict; $[ = 2; my @num = ( 1..4 ); print "No of elements >> ", $#num+1, "\n"; print "No of elements >> ", scalar @num, "\n";

Ouput

No of elements >> 6
No of elements >> 4

 $# is actually the last subscript of the array and the variable  $[ sets the base subscript to 2 (default is zero). To access the elements in @num you will have to use subscripts 2,3,4,5 and that is the reason you get '6' in the first case (5+1) and not '4' which is the actual number of elements.

Agree that when you dont reset $[ both the methods are same. Why use a complex last subscript+1 calculation when there is an easier way to find the number of elements that works in *all* cases ??

-T

In reply to Re: Grade me! TAC by thens
in thread Grade me! TAC by Frank_Zappa_lives

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.