Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 588483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:19:23+00:00 2026-05-13T15:19:23+00:00

The number of values in a list can only be determined by iterating over

  • 0

The number of values in a list can only be determined by iterating over its values, or converting it to an array. Assigning it to a scalar won’t return the items count:

my $n = ('a', 'b', 'c');  # $n = 'c' 

There’s an “empty parentheses” idiom, that can be used to get the number of elements:

my $n = () = ('a', 'b', 'c'); # $n = 3

Is it equivalent internally to

my $n = @{[ 'a', 'b', 'c' ]};

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:19:23+00:00Added an answer on May 13, 2026 at 3:19 pm

    This is an interesting implementation detail: Does the assignment to an empty list create an (unnecessary) anonymous array?

    There are two ways of answering this question: First, The Right Way: Try to figure out how this might be handled in the source. Is there a special case for assignment to an empty list evaluated in scalar context?

    Being the lazy and ignorant type, I chose to use Benchmark:

    #!/usr/bin/perl
    
    use strict; use warnings;
    use Benchmark qw( cmpthese );
    
    cmpthese -5,  {
        goatse => sub { my $n = () = 'A' .. 'Z' },
        anon   => sub { my $n = @{[ 'A' .. 'Z' ]}},
    };
    

    I ran the benchmark a bunch of times, and the assignment to empty list had a slight advantage in all cases. If the difference were purely random, the probability of observing 10 timings all in favor of goatse is less than 0.1%, so I am assuming there is some kind of short circuit.

    On the other hand, as running the benchmark @daotoad posted in the comments, probably gives a more complete picture:

    #!/usr/bin/perl
    
    use strict; use warnings;
    use Benchmark qw( cmpthese );
    
    use constant START => 1;
    use constant STOP => 1000;
    
    my $start = START;
    my $stop = STOP;
    
    cmpthese -5, {
        anon => sub { my $n = @{[ $start .. $stop ]}},
        goatse => sub { my $n = () = $start .. $stop },
        canon => sub { my $n = @{[ START .. STOP ]}},
        cgoatse => sub { my $n = () = START .. STOP },
    };
    

    Typical results on my machine (Windows XP Pro SP3, Core 2 Duo, 2 Gb memory, ActiveState perl 5.10.1.1006):

               Rate    anon cgoatse  goatse   canon
    anon     5207/s      --    -45%    -49%    -51%
    cgoatse  9522/s     83%      --     -7%    -10%
    goatse  10201/s     96%      7%      --     -4%
    canon   10636/s    104%     12%      4%      --

    And, with:

    use constant START => 'AAAA';
    use constant STOP => 'ZZZZ';
    

    the results are:

              Rate    anon  goatse cgoatse   canon
    anon    1.73/s      --    -12%    -16%    -17%
    goatse  1.98/s     14%      --     -4%     -5%
    cgoatse 2.06/s     19%      4%      --     -1%
    canon   2.08/s     20%      5%      1%      --

    Conclusion:

    If in doubt, use my $n = () = ...;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I check the number of values in an array, for example... $abc=array(); $abc[0]=asd;
Consider the following code: List<double> l = new List<double>(); //add unknown number of values
I would like to know how I can count the number of unique values
I have a number of text input elements that when its values are changed,
I am trying to return a list of ungrouped values having a max defined
I'm looking for the most elegant way to count the same number values in
I am trying to generate nth bell number for large values between 500 to
I am writing the query to count the number of distinct values in the
I have a string that contains a known number of double values. What's the
I am using Zend_Oauth_Consumer which requires a number of config values to be passed.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.