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

Counting elements in array

by finfan (Acolyte)
on Jan 29, 2014 at 19:29 UTC ( #1072566=perlquestion: print w/replies, xml ) Need Help??
finfan has asked for the wisdom of the Perl Monks concerning the following question:

I know this has got to have been asked before. And a search of the web gives me much the same answers, but I must be missing something.

I understand that the following:

print "Array size is @array\n";

should tell me the number of elements in the array called @array. Yet, it doesn't, it prints the elements, in order. So, how do I determine exactly how many elements have been stored in an array?

Replies are listed 'Best First'.
Re: Counting elements in array
by davido (Archbishop) on Jan 29, 2014 at 19:38 UTC

    When you incorporate @array into a double-quoted construct it is interpolated as a list of elements, rather than with scalar context. The list separator in double-quoted constructs is usually " " (the space character), but can be changed to just about anything by setting $".

    So you might think, "Well, I'll just do this: print @array, "\n";.", which would be wrong again, because once again @array is being evaluated in list context, which just returns the elements (this time separated by whatever is set in "$,".

    You have to give @array scalar context. Here are a couple of ways:

    my $size = @array; print "$size\n"; # or... print scalar(@array), "\n"; # or .. print @array . "\n"; # Because '.' invokes scalar context on its opera +nds.


Re: Counting elements in array
by thezip (Vicar) on Jan 29, 2014 at 19:35 UTC

    You have (at least) a couple of options:

    print "Array size is ", scalar(@array), "\n";
    - or -
    printf "Array size is %d\n", scalar @array;

    Your pick. You need to force scalar context, which I explicitly did by using the scalar function.

    *My* tenacity goes to eleven...
Re: Counting elements in array
by keszler (Priest) on Jan 29, 2014 at 19:31 UTC
    print "Array size is " . scalar @array . "\n";
Re: Counting elements in array
by boftx (Deacon) on Jan 29, 2014 at 19:32 UTC

    Context is everything. Used in a quoted string, "@array" will print the elements. You must use it in a numeric context (i.e. if ( @array > 0 )) to have it behave that way.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Counting elements in array
by GrandFather (Sage) on Jan 29, 2014 at 21:55 UTC

    You may have noticed that Perl uses sigils in front of variable names? They aren't part of the variable name, they show that type of thing the variable (or expression) returns. So a $ sigil returns a scaler 'value' - regardless of how that 'value' is being used. An @ returns a list. A % returns a hash (which is a list of key value pairs). Sigils are more like conversion operators than part of variable identifiers.

    If the code changes take longer than the time saved, it's fast enough already.
Re: Counting elements in array
by simmisam (Novice) on Jan 29, 2014 at 20:42 UTC

    This might be the most easiest question in perl. You can get array size by any of the following

    my $size = @array; print $size;


    print scalar @array;
Re: Counting elements in array
by vinoth.ree (Monsignor) on Jan 30, 2014 at 05:05 UTC

    I prefer the following

    my @Array = (1,2,3,4); say 0+@Array; #@Array as a number)

    Its clearer than

    say scalar(@Array);  #@Array as a scalar)

    If you just want to print the size, this is the simplest way.

    print "size of array: " . @array . ".\n";

    All is well
Re: Counting elements in array
by Discipulus (Monsignor) on Jan 30, 2014 at 08:39 UTC
    you can find interesting the fact that $#array_name contain the index of the last element so that ($#array_name + 1) == scalar @array_name.

    As stated wisely by some monk, in Perl context is all, as in human languages, this is powerful but not-so-easy-to-manage feature. Consider a read of context in the Modern Perl book. This book is a gem but i dont suggest as a first Perl read: i prefer Perl Cookbook, albeit a little ancient book but a wonderful introduction by examples to many fields where Perl rocks.


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Counting elements in array
by nithins (Sexton) on Jan 30, 2014 at 06:52 UTC

    Try this !!

    print "The Array size is!:: scalar (@array) \n";

      Try this trick to evaluate inside quotation string:

      print "Array has @{[0+@Array]} elements"; print "Hash has @{[0+keys %Hash]} keys";

      Sorry for necrobumping ^))

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1072566]
Approved by boftx
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-05-21 21:44 GMT
Find Nodes?
    Voting Booth?