Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Deleting specific array elements

by JavaFan (Canon)
on Nov 26, 2008 at 10:47 UTC ( [id://726065]=note: print w/replies, xml ) Need Help??


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

Unfortunally, if the array is large, and there's a significant number of elements "\n" in the array, the repeated splicing can be slow. splice() is an O(n) operation (with n the length of the array), and that's a tight worst case bound (splice may require shifting half the array). If you have a large array, and cannot afford to copy, you can still remove the offending elements in situ:
my $j = 0; for (my $i = 0; $i < @molecules; $i ++) { $molecules[$j++] = $molecules[$i] unless $molecules[$i] eq "\n"; } splice @molecules, $j;

Replies are listed 'Best First'.
Re^4: Deleting specific array elements
by matrixmadhan (Beadle) on Nov 26, 2008 at 15:50 UTC
    for (my $i = 0; $i < @molecules; $i ++) {

    Should n't that be as

    for (my $i = 0; $i < scalar(@molecules); $i ++) {
      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! :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2026-04-21 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.