Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: Deleting specific array elements

by matrixmadhan (Beadle)
on Nov 26, 2008 at 15:50 UTC ( [id://726137]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Deleting specific array elements
in thread Deleting specific array elements

for (my $i = 0; $i < @molecules; $i ++) {

Should n't that be as

for (my $i = 0; $i < scalar(@molecules); $i ++) {

Replies are listed 'Best First'.
Re^5: Deleting specific array elements
by JavaFan (Canon) on Nov 26, 2008 at 17:05 UTC
    Why? < provides scalar context to its operands.
      Thanks for that and am not aware of such a context reasoning

      One more question
      for (my $i = 0; $i < @molecules; $i ++) {
      Instead of the above
      my $num_elements = scalar(@molecules); for (my $i = 0; $i < $num_elements; $i++) {

      will that bring any performance improvement or the number of elements of an array is stored internally ( in some scalar ) and there is no need to evaluate it again ?

      Thanks again for the reply! :)
        There's no significant speed difference. For both the scalar and the array, the get to the integer value from the SV or AV, one has to walk a pointer and add an offset.

        It may even be that in the scalar case, perl checks IOK flag each time (to see whether it actually has a valid integer value), but that maybe optimized away.

        Anyway, there isn't much of a reason to store the length of an array in a seperate variable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found