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

Im new to Perl from a php background. Now, as you know PHP has approximately 5 billion and 1 built in functions to do literally everything. Approximately 4 billion of them are ways of dealing with arrays. I've been crawling through perl documentation, and I can't find a perl equivalent to PHP's count($array) function which returns how many items there are in an array. (I know how to iterate through the array and count that way, I was hoping for a built in function.)
  • Comment on perl equivalent to php array count function

Replies are listed 'Best First'.
Re: perl equivalent to php array count function
by dragonchild (Archbishop) on Jun 14, 2004 at 03:02 UTC
    atcroft and Zaxo have given you the correct answer. However, the answer may, itself, be confusing.

    Perl, unlike most languages, has the concept of "context", which is exactly what it says. Every single Perl thing may behave differently if it is used in a different ways. There are four basic contexts.

    1. Scalar - using something as a value
    2. List - using something as part of a group
    3. Void - using something without caring about its return value (primarily used in determining how a function is used)
    4. String - using something as a string (primarily used as the key in a hash)

    This means that, to learn how to use something, you need to know how it behaves in various contexts. :-)

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

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: perl equivalent to php array count function
by davido (Cardinal) on Jun 14, 2004 at 03:59 UTC
    I just wanted to add a few thoughts.

    Perl has about 5 billion things it can do too, but many of them are built into the nuances of its rich syntax, some are functions, some are functions evaluated in various contexts, and others are brought to you by virtue of pragmas and core modules. Of course CPAN is a whole other chapter in the book... but I digress...

    An array, evaluated in scalar context, will return its number of elements. Therefore:

    my @array = qw/one two three four five/; print scalar @array; __OUTPUT__ 5

    There are a few other tricks you can do with arrays too...

    my @array = qw/one two three four five/; print $#array; __OUTPUT__ 4

    Explanation: Arrays start with the sigil @. @array, for example, is an array. And if @array is an array, $#array is a special syntax used to determine the index of the last element of an array. If @array has five elements, and you start counting from zero (the index of the first element), the index of the last element is 4. And that's the number you get when you evaluate $#array.

    my @array = qw/one two three four five/; $array[2] = undef; # undefine one of the elements. print scalar grep { defined $_ } @array; __OUTPUT__ 4

    Here you're using grep in scalar context to count how many elements in @array actually contain a value. There are five elements in @array, the last index is 4, and because we've erased the contents of $array[2], despite there being five elements, only four of them hold a value, so the output is 4.

    my @array = qw/one two three four five/; print $array[-1]; __OUTPUT__ five

    Here you're indexing into @array, but asking for the element with the index of -1. When you ask for negative indices, Perl starts at the end of the array and counts backwards. So in a five element array, $array[4] (the fifth element) is the same thing as $array[-1] (still the fifth element).

    my @array = qw/one two three four five/; $#array = 99; print scalar @array; __OUTPUT__ 100

    Huh? What you've just done here is pre-extend the five element array named @array out to one hundred elements (95 of them just contain 'undef' (no value)).

    Perlish arrays are also easily usable as FIFO queues, LIFO stacks, double-ended stacks, and so on, via push, pop, shift, unshift. ...oh, and don't forget splice.

    Just wanted to offer a few tidbits of Perl's rich syntax and a few functions that expand the horizons of that syntax. Most of this isn't immediately helpful to your question, but just demonstrates a few other very useful tricks.

    Enjoy.


    Dave

      There should be a way of tagging certain comments (like these previous) as part of the basic concepts that perl students should read at this site.

      It helps a lot to read your comments!

      Shouldn't they be 'soft-linked' to a Basic Tutorial series?

      .{\('v')/}
      _`(___)' __________________________
        No. Tutorials are special, and this kind of information is covered in every introduction to perl datastructures (even perldata).
      Congrats and ++'s to davido.

      Man, that's what I call an explanation!

      davido, you should consider making a tutorial based on this, something like "Dealing with arrays".

      Best regards,

      my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");
      davido, that was awesome thanks. Thanks to everyone in fact for the help. I can see that Perl is going to be an exciting adventure.
Re: perl equivalent to php array count function
by atcroft (Abbot) on Jun 14, 2004 at 02:52 UTC

    If you look at an array in a scalar context, it will return the number of items:

    my @array = (1, 4, 9, 16); print scalar(@array); # prints 4

    Hope that helps.

Re: perl equivalent to php array count function
by Zaxo (Archbishop) on Jun 14, 2004 at 02:54 UTC

    In Perl, the array itself in scalar context gives the number of elements:

    my @foo = qw/foo bar baz/; my $foocount = @foo; print $foocount, $/; print scalar(@foo), $/;
    No function necessary.

    After Compline,
    Zaxo