Thread overview |
---|
August 09, 2015 Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
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 |
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reflexive | 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 you
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.
|
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | 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 ? |
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reflexive | On 10/08/2015 2:24 a.m., Reflexive wrote:
> 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 ?
You sure you are using it correctly (off top of my head)?
-----
import std.algorithm : remove;
...
arr.remove(e);
-----
|
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reflexive | 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 read
remove() is a function in module std.algorithm:
import std.algorithm: remove;
remove(foo);
|
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinke | 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. |
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reflexive | 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) [1]: http://dlang.org/phobos/std_random.html#.randomShuffle |
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Parrill | 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. π
|
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reflexive | 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);
|
August 09, 2015 Re: Removing elements from dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reflexive | 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. |
Copyright © 1999-2021 by the D Language Foundation