Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: How do I find the size of an array?

by panda.umesh9 (Initiate)
on Feb 09, 2010 at 17:55 UTC ( [id://822246]=note: print w/replies, xml ) Need Help??


in reply to How do I find the size of an array?

$length=($#array+1)
  • Comment on Re: How do I find the size of an array?

Replies are listed 'Best First'.
Re: Answer: How do I find the size of an array?
by DrHyde (Prior) on Feb 10, 2010 at 11:00 UTC
    $#array + 1 is wrong. $#array is the index of the last element in @array. It is *normally* one less than the number of elements in the array, but not always, as it assumes that array indices start at 0. If $[ is set then that is not necessarily the case.
Re: Answer: How do I find the size of an array?
by VinsWorldcom (Prior) on Feb 09, 2010 at 21:02 UTC

    What's wrong with:

    $length = @array;

    For example:

    #!/usr/bin/perl use strict; my @array = qw(one two three); my $length = $#array + 1; print "Dollar pound array = $length\n"; $length = @array; print "At symbol array = $length\n"; __DATA__ {C} > test.pl Dollar pound array = 3 At symbol array = 3
      What's wrong with:
      $length = @array;
      Nothing. TIMTOWTDI.

      -- Randal L. Schwartz, Perl hacker

      The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-25 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found