Jump to page: 1 2
Thread overview
Removing elements from dynamic array
Aug 09, 2015
Reflexive
Aug 09, 2015
cym13
Aug 09, 2015
Reflexive
Aug 09, 2015
Rikki Cattermole
Aug 09, 2015
kinke
Aug 09, 2015
Reflexive
Aug 09, 2015
Alex Parrill
Aug 09, 2015
Reflexive
Aug 09, 2015
anonymous
Aug 09, 2015
drug
Aug 09, 2015
Reflexive
August 09, 2015
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
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
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
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
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
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
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
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
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
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.
« First   ‹ Prev
1 2