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

Re: Re: random number

by BrowserUk (Patriarch)
on Dec 13, 2002 at 04:22 UTC ( [id://219495]=note: print w/replies, xml ) Need Help??


in reply to Re: random number
in thread random number

Given that you know the index of the item you want to remove from the array, it would be much more efficient to use splice instead of grep to remove the item from the array.

#replace my @newarray = grep {$_ !=$element} @array; #remove the element #with splice @array, $index, 1; #remove item from array; # and remove this. No need as the array has been modified. # @array = @newarray; #reset the array

Examine what is said, not who speaks.

Replies are listed 'Best First'.
Re: Re: Re: random number
by I0 (Priest) on Dec 14, 2002 at 23:35 UTC
    #splice is still O(n) $array[$index] = $array[-1]; pop @array;

      True. And that's a neat solution.

      It would still be better to shuffle the array and simply pop the next one when you need it as was suggested at near the top of the thread.


      Examine what is said, not who speaks.

Log In?
Username:
Password:

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

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

    No recent polls found