in reply to Finding the total number of elements in an array.

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...
  • Comment on Re: Finding the total number of elements in an array.

Replies are listed 'Best First'.
RE: Re: Finding the total number of elements in an array.
by ferrency (Deacon) on Jul 19, 2000 at 19:37 UTC
    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...