digitalmars.D.learn - Removing elements from dynamic array
- Reflexive (7/7) Aug 09 2015 Hi
- cym13 (4/11) Aug 09 2015 You can use std.algorithm.remove for that. If you need more
- Reflexive (7/11) Aug 09 2015 I get :
- Rikki Cattermole (7/19) Aug 09 2015 You sure you are using it correctly (off top of my head)?
- kinke (4/6) Aug 09 2015 remove() is a function in module std.algorithm:
- Reflexive (50/50) Aug 09 2015 I wrote import std.algorithm.remove ;, and I dont have any longer
- Alex Parrill (11/15) Aug 09 2015 Why not just use std.random.shuffle [1]?
- Reflexive (35/36) Aug 09 2015 Well, I didn't knew about it, that's the reason ...
Hi I have seen that it is possible to remove an element from a associative array, but dont find any reference, including in D's specification, about any element removing in dynamic array. I can use loops for that, but isnt any way to simple reduce a dynamic array size ? Thank you
Aug 09 2015
On Sunday, 9 August 2015 at 13:22:02 UTC, Reflexive wrote:Hi I have seen that it is possible to remove an element from a associative array, but dont find any reference, including in D's specification, about any element removing in dynamic array. I can use loops for that, but isnt any way to simple reduce a dynamic array size ? Thank youYou can use std.algorithm.remove for that. If you need more advanced ways to remove elements, you may want to switch from a regular dynamic array to a std.container.array.
Aug 09 2015
On Sunday, 9 August 2015 at 13:40:51 UTC, cym13 wrote:On Sunday, 9 August 2015 at 13:22:02 UTC, Reflexive wrote: You can use std.algorithm.remove for that. If you need more advanced ways to remove elements, you may want to switch from a regular dynamic array to a std.container.array.I get : Error: module remove is in file 'std/algorithm/remove.d' which cannot be read import path[0] = /usr/include/dmd/phobos import path[1] = /usr/include/dmd/druntime/import I'm in Linux Mint 17 ... maybe an permission problem ?
Aug 09 2015
On 10/08/2015 2:24 a.m., Reflexive wrote:On Sunday, 9 August 2015 at 13:40:51 UTC, cym13 wrote:You sure you are using it correctly (off top of my head)? ----- import std.algorithm : remove; ... arr.remove(e); -----On Sunday, 9 August 2015 at 13:22:02 UTC, Reflexive wrote: You can use std.algorithm.remove for that. If you need more advanced ways to remove elements, you may want to switch from a regular dynamic array to a std.container.array.I get : Error: module remove is in file 'std/algorithm/remove.d' which cannot be read import path[0] = /usr/include/dmd/phobos import path[1] = /usr/include/dmd/druntime/import I'm in Linux Mint 17 ... maybe an permission problem ?
Aug 09 2015
On Sunday, 9 August 2015 at 14:24:38 UTC, Reflexive wrote:Error: module remove is in file 'std/algorithm/remove.d' which cannot be readremove() is a function in module std.algorithm: import std.algorithm: remove; remove(foo);
Aug 09 2015
I wrote import std.algorithm.remove ;, and I dont have any longer
any error. But, it dont want to remove the item. Here is the
source, the problem is in the shuffle() method :
// sabot.d
// version 0.0.1
import std.stdio ;
import std.random ;
import std.algorithm : remove ;
void main(){
auto deck = new sabot ;
deck.shuffle() ;
class sabot{
card[] sabotarray ;
(...)
struct card {
int id ;
(...)
}
(...)
this(){
(the constructor builds up the array 'sabotarray'.)
}
void shuffle(){
card[] tempsabotarray ;
card tempcard ;
long id_card ;
int counter ;
while(this.sabotarray.length>1){
if(counter>=60){break;}
id_card = uniform(0,this.sabotarray.length-1) ;
tempcard = this.sabotarray[id_card] ;
this.sabotarray.remove(id_card) ;
tempsabotarray ~= tempcard ;
writeln ("Counter : " , counter, " ; id card : " , id_card, "
; sabot size : ", this.sabotarray.length) ;
++counter ;
}
this.sabotarray = tempsabotarray ;
}
(...)
}
And I get in the terminal :
$ dmd -run sabot.d
Counter : 0 ; id card : 18 ; sabot size : 52
Counter : 1 ; id card : 40 ; sabot size : 52
Counter : 2 ; id card : 14 ; sabot size : 52
Counter : 3 ; id card : 44 ; sabot size : 52
(...)
Until counter is 60. Sabot size remains 52 all the time.
Without the break statement, the computer goes out of memory.
Aug 09 2015
On Sunday, 9 August 2015 at 15:30:32 UTC, Reflexive wrote:I wrote import std.algorithm.remove ;, and I dont have any longer any error. But, it dont want to remove the item. Here is the source, the problem is in the shuffle() method : ...Why not just use std.random.shuffle [1]? import std.random; void shuffle() { randomShuffle(this.sabotarray); } (Also, this is more of a style opinion, but please don't define all your variables up front; define them when they're first used. The other way IMO has too much cognitive load, and you can't use `auto` in many cases)
Aug 09 2015
On Sunday, 9 August 2015 at 15:41:33 UTC, Alex Parrill wrote:Why not just use std.random.shuffle [1]?Well, I didn't knew about it, that's the reason ... For the shuffle method, it is certainly the best to do, but I need the remove() method at other places. I see that remove() removes the value of the element but keeps the same size of the array, and replace the value by a new one at the end. The method : class sabot{ card[] sabotarray ; (...) card getCard(){ card returncard = this.sabotarray[0] ; this.sabotarray.remove(0) ; return returncard ; } The sabotarray before taking away cards : $ dmd -run sabot.d 0. 🂡 1. 🂢 2. 🂣 ... 48. 🃚 49. 🃛 50. 🃝 51. 🃞 The sabotarray after removing the two first cards : $ dmd -run sabot.d 0. 🂣 1. 🂤 2. 🂥 ... 47. 🃛 48. 🃝 49. 🃞 50. 🃞 51. 🃞
Aug 09 2015
On Sunday, 9 August 2015 at 20:23:00 UTC, Reflexive wrote:
I see that remove() removes the value of the element but keeps
the same size of the array, and replace the value by a new one
at the end. The method :
class sabot{
card[] sabotarray ;
(...)
card getCard(){
card returncard = this.sabotarray[0] ;
this.sabotarray.remove(0) ;
return returncard ;
}
`remove` doesn't update the passed array, but it returns a slice
of it that doesn't include the removed element. To update the
original array, assign the result of `remove` to it:
this.sabotarray = this.sabotarray.remove(0);
Aug 09 2015
09.08.2015 23:22, Reflexive пишет:Try to use this.sabotarray = this.sabotarray.remove(id_card); remove() removes element(s) but doesn't change length of 'old' array. To get new length you should use 'new' array that returned from remove(). In this case I get rid of two excessive kings in cards.
Aug 09 2015
On Sunday, 9 August 2015 at 20:38:05 UTC, drug wrote:09.08.2015 23:22, Reflexive пишет:Thank you to both anonymous and drug, that was the solution.Try to use this.sabotarray = this.sabotarray.remove(id_card); remove() removes element(s) but doesn't change length of 'old' array. To get new length you should use 'new' array that returned from remove(). In this case I get rid of two excessive kings in cards.
Aug 09 2015









Rikki Cattermole <alphaglosined gmail.com> 