in reply to Count the number of parameters

Either evaluate @ARGV in scalar context:

$nargs = @ARGV;

or do this:

$nargs = $#ARGV + 1;

Replies are listed 'Best First'.
Re: Re: Count the number of parameters
by Juerd (Abbot) on Jun 02, 2003 at 05:25 UTC

    $nargs = $#ARGV + 1;

    Don't do that. $#foo + 1 isn't necessarily equal to @foo, but that's not the most important reason for not using it. You shouldn't use it because it is not what you meant.

    If you want the number of elements, GET the number of elements. If you want the last index, GET the last index. Don't get the last index if you need the number of elements and don't get the number of elements to calculate the last index.

    In short: use @foo if you mean @foo, use $#foo if you mean $#foo. Don't use @foo - 1 or $#foo + 1.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: Re: Count the number of parameters
by Massyn (Hermit) on Jun 02, 2003 at 03:39 UTC

    I just found scalar(@whatever) in perldata ;-) Same thing! Thanks!

      FWIW, you don't need to use scalar, as my $count = @some_array; will suffice.

      -- vek --