Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Lagrange Polynomials in Perl

by BrowserUk (Patriarch)
on Apr 28, 2015 at 20:42 UTC ( [id://1125064]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Lagrange Polynomials in Perl
by Mascasc (Novice) on Apr 29, 2015 at 14:36 UTC
    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
        That is actually much more efficient, thank you. I will use that from now on.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1125064]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found