Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Reporting

by kelan (Deacon)
on Apr 03, 2003 at 18:28 UTC ( #247841=note: print w/replies, xml ) Need Help??


in reply to Reporting

my $num_elements = @array; is not the same as my $num_elements = $#array; The second will give you one less than the length because it actually returns the index of the last element.

kelan


Perl6 Grammar Student

Replies are listed 'Best First'.
Re: Re: Reporting
by dragonchild (Archbishop) on Apr 03, 2003 at 19:10 UTC
    I think the question was more of which of the following is faster:
    if ($#array >= 0) ### or ... if (@array > 0)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Both of those are better written as if (@array) {. Its likely to be less ops as well.

      Im actually thinking the question was.

      Which is faster?
      $num = scalar(@array); # or $num = $#array;
      And as someone pointed out $#array isnt acurate as to the actual number of elements in the array, so I guess you could simply compare the op trees of the two. Or I guess perform 3 benchmarks,
      1) plain
      2) scalar(@array) - 1
      3) $#array + 1;

      /* And the Creator, against his better judgement, wrote man.c */

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://247841]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2022-12-05 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?