www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assosiative array pop

reply "seany" <seany uni-bonn.de> writes:
Given an assosiative array : int[string] k, is there a way 
(either phobos or tango) to pop the first element of this array 
and append it to another array?

I can come up with a primitive soluiton:

int[string] k;
// populate k here

int[string] j;


foreach(sttring key, int val; k)
{

j[key] = val;
break;
}

but could it be better? it is wroth noting that the keys are not 
known beforehand.
Jun 25 2014
next sibling parent "seany" <seany uni-bonn.de> writes:
Aso, I wanted to mention that I did not find much info in the 
manual page on this.
Jun 25 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
seany:

 int[string] k;
 // populate k here

 int[string] j;


 foreach(sttring key, int val; k)
 {

 j[key] = val;
 break;
 }
This is OK, and you can encapsulate this into a little function. But you are not removing the pair from the first associative array. So you have to remove the item before the break. Bye, bearophile
Jun 25 2014
prev sibling next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Wednesday, June 25, 2014 09:30:48 seany via Digitalmars-d-learn wrote:
 Given an assosiative array : int[string] k, is there a way
 (either phobos or tango) to pop the first element of this array
 and append it to another array?

 I can come up with a primitive soluiton:

 int[string] k;
 // populate k here

 int[string] j;


 foreach(sttring key, int val; k)
 {

 j[key] = val;
 break;
 }

 but could it be better? it is wroth noting that the keys are not
 known beforehand.
There's no such thing as the "first" element of an AA. An associative array is a hash table and has no order to it. The order you get when iterating with foreach is undefined. If you just want to get _a_ key from an AA, then you need to either iterate over it with foreach and then break like you're doing or use byKey to get a range. e.g. something like auto key = k.byKey().front; j[key] = k[key]; should work. But there is no "first" key, so I don't know really understand what you're really trying to do here and can't provide a better answer without more information. - Jonathan M Davis
Jun 25 2014
prev sibling parent reply "Meta" <jared771 gmail.com> writes:
On Wednesday, 25 June 2014 at 09:30:54 UTC, seany wrote:
 Given an assosiative array : int[string] k, is there a way 
 (either phobos or tango) to pop the first element of this array 
 and append it to another array?

 I can come up with a primitive soluiton:

 int[string] k;
 // populate k here

 int[string] j;


 foreach(sttring key, int val; k)
 {

 j[key] = val;
 break;
 }

 but could it be better? it is wroth noting that the keys are 
 not known beforehand.
If you want something like a hash table that preserves insertion order, you could try using an array of tuples instead. Then to "pop" the first element, just do 'arr = arr[1..$]'.
Jun 25 2014
parent reply "seany" <seany uni-bonn.de> writes:
On Wednesday, 25 June 2014 at 14:17:50 UTC, Meta wrote:

 If you want something like a hash table that preserves 
 insertion order, you could try using an array of tuples 
 instead. Then to "pop" the first element, just do 'arr = 
 arr[1..$]'.
Thank you, this is _exactly_ what I was looking for! Thank you so much!
Jun 26 2014
next sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
On Wednesday, 25 June 2014 at 14:17:50 UTC, Meta wrote:
 Then to "pop" the first element, just do 'arr = arr[1..$]'.
Or import std.array to get the range primitives for slices: import std.array; void main() { auto arr = [1, 2, 3, 4]; arr.popFront(); assert(arr.front == 2); }
Jun 26 2014
prev sibling parent "Meta" <jared771 gmail.com> writes:
On Thursday, 26 June 2014 at 09:21:28 UTC, seany wrote:
 On Wednesday, 25 June 2014 at 14:17:50 UTC, Meta wrote:

 If you want something like a hash table that preserves 
 insertion order, you could try using an array of tuples 
 instead. Then to "pop" the first element, just do 'arr = 
 arr[1..$]'.
Thank you, this is _exactly_ what I was looking for! Thank you so much!
Keep in mind that it will be up to you to ensure that each value only exists in the array once.
Jun 26 2014