Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hej All, Is there something similar to lbound or ubound in Perl? I have tokenized an delimited string into an array but as the string is variable length, the total number of elements in the array is non constant. How can I get the total number of elements in an array? - Jed
  • Comment on Finding the total number of elements in an array.

Replies are listed 'Best First'.
Re: Finding the total number of elements in an array.
by dempa (Friar) on Jul 19, 2000 at 15:46 UTC
    In scalar context, the array returns the number of elements.
    my @colors = qw/red green blue cyan/; print "I have " . scalar @colors . " colors\n";
(jjhorner)Finding the total number of elements in an array.
by jjhorner (Hermit) on Jul 19, 2000 at 16:15 UTC

    You can get the index of the last element of the array by:

    $#arrayname

    Which can be used like this:

    my $i; for ($i = 0; $i <= $#arrayname; $i++) { #do something }

    And getting the total number of elements is as easy as:

    my @stooges = qw(Larry Moe Curly); print scalar(@stooges), " Stooges!\n";
    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
Re: Finding the total number of elements in an array.
by le (Friar) on Jul 19, 2000 at 15:47 UTC
Re: Finding the total number of elements in an array.
by eduardo (Curate) on Jul 19, 2000 at 17:22 UTC
    part of me wants to vote this question down. let me explain, i have absolutely no problems with people asking what might be termed "newbie" questions. however, let's analyze this one, this question could have been self answered by looking at man perldata and quickly scanning it we find:
           
    If you evaluate a named array in a scalar context, it
    returns the length of the array.  (Note that this is not
    true of lists, which return the last value, like the C
    comma operator, nor of built-in functions, which return
    whatever they feel like returning.)  The following is
    always true:
    
               scalar(@whatever) == $#whatever - $[ + 1;
    (stuff deleted) 
    So in general you can assume that
    
               scalar(@whatever) == $#whatever + 1;
    
    Some programmers choose to use an explicit conversion so
    nothing's left to doubt:
    
               $element_count = scalar(@whatever);
    
    
    which is about as definitive an answer as you are going to get! maybe this is the "angry old man" in me coming out, but it kind of ticks me off when it feels like maybe someone didn't research things enough before posting a question... oh well... i won't vote this one down, but i really hope that perlmonks doesn't start getting flooded (like the MySQL mailing list and most other good tech forums that exist) with questions that could have been easilly solved by a quick scan through some documentation...
      I somewhat agree with you: I don't want PerlMonks to start being flooded with the same basic questions over and over.

      However, Almost All questions about Perl syntax and things like this, even the confusing ones, can validly be answered with "RTFM." This doesn't mean that the answer in the manual is adequate to answer someone's question. It also implies that the asker knows where the manual is.

      For example: if you were to tell a perl newbie with experience programming in C, "RTFM: It says right here, evaluate the array in a scalar context to get its length," that may be No Help At All, since context is a completely foreign concept to many C programmers.

      I think a better attitude to take would be to reinterpret "How can I <basic syntax issue>" as "Where is the documentation for <basic syntax issue>?" If you teach the person where to find the answer, instead of telling them they shouldn't have asked the question, then maybe they will read the manual before posting in the future, now that they know where it is.

      If you don't want to waste your time answering the question, don't do it. There are other monks less experienced than you who would be happy to help.

      That said: there is a good answer to this question in the categorized Q&A section of Perl Monks as well: How do I find the size of an array?

      As for array lengths: often you don't need them at all. If you're planning to loop over the elements of an array, often it's better practice to use a foreach style loop than a c-style loop:

      my @array = qw(a few items in the array); foreach my $element (@array) { do_something_with($element); }
      Alan
        bravo. i see your point, and you have a very compelling argument. it's often times hard to remember what it's like to know that the answer is plain and simple and there... yet for some reason, you can not find it. so, my suggestion is, maybe there should be a multi-channel setup, whereby some group of users are able to rate a question on the level of knowledge required to answer such a question? a question like this would have a rating of novice, and therefore anyone who was too damned high and mighty (making fun of myself here...) to answer novice questions, ('cause you know, we'd never been novices :) ) would just be able to ignore them...
RE: Finding the total number of elements in an array.
by Russ (Deacon) on Jul 19, 2000 at 19:55 UTC
RE: Finding the total number of elements in an array.
by mrmick (Curate) on Jul 19, 2000 at 18:08 UTC
    One solution is to get the index number of the last element and add one. We use $# for this:
    # not very imaginative, but hopefully it helps @array = qw(one two three four); # get the number of elements $number_of_elements = $#array +1; print $number_of_elements;
    OUTPUT: 4

    Mick