in reply to Re: Lagrange Polynomials in Perl
in thread Lagrange Polynomials in Perl

Does $scalar = @array produce a scalar containing the size of the array?

Replies are listed 'Best First'.
Re^3: Lagrange Polynomials in Perl
by BrowserUk (Patriarch) on Apr 28, 2015 at 20:42 UTC
    Does $scalar = @array produce a scalar containing the size of the array?

    Yes. But $point is a strange name for the size of an array!

    If that is meant to be short for $lastPoint, it is still deceptive as with zero-based arrays, the index of the last point in the array is one less than the size of the array.

    (Which incidentally, can be obtained using my $lastPoint = $#array;)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      Yeah, I was thinking of it as the stopping point for the for loop more than the last point, although you are right...it's pretty unclear.

        In general, if I need to use C-style for loops to traverse arrays, I simply put the @array limit directly in the condition:

        for( my $i = 0; $i < @array; ++i ) { ...; }

        I realise that you were probably thinking that as you have two nested loops running to the same limit, it might be more efficient to do the size 'calculation' before both and use a scalar; but the reality is that there is no calculation being done, the size of the array represented by @array is simply a direct reference to an internal integer, so the cost is actually less than referencing the integer value of a scalar variable.

        The difference isn't big enough to worry about for most purposes:

        cmpthese -1,{ a=>q[ my @a = 1 .. 1e3; for( my $i = 0; $i < @a; ++$i ){ for( my$j = 0; $j < @a; ++$j ){ 1; } } ], b=>q[ my @a = 1 .. 1e3; my $l = @a; for( my $i = 0; $i < $l; ++$i ){ for( my $j = 0; $j < $l; ++$j ){ 1; } } ] };; Rate a b a 5.81/s -- -41% b 9.84/s 69% -- [0] Perl>

        But the point is that there is little purpose in assigning the size of an array to a scalar variable, just reference it directly when you need it.

        Ditto for the length of a perl string.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re^3: Lagrange Polynomials in Perl
by Laurent_R (Canon) on Apr 28, 2015 at 20:25 UTC
    Yes, it does, but I would rather write:
    my $array_count = scalar @array;
    or, omitting the scalar built-in unnecessary in scalar context:
    my $array_count = @array;
    But I often prefer to add the scalar built-in, even when not necessary, just to make my intent clearer to the maintainer (which, incidentally, one year from now, might be me, and such indication might prove useful to help me understand the intent of the code).

    Update: removed the scalar word which I had forgotten to omit in the second snippet. Thanks to Athanasius for pointing it out.

    Je suis Charlie.