digitalmars.D.learn - Assosiative array pop
- seany (14/14) Jun 25 2014 Given an assosiative array : int[string] k, is there a way
- seany (2/2) Jun 25 2014 Aso, I wanted to mention that I did not find much info in the
- bearophile (6/14) Jun 25 2014 This is OK, and you can encapsulate this into a little function.
- Jonathan M Davis via Digitalmars-d-learn (12/26) Jun 25 2014 There's no such thing as the "first" element of an AA. An associative ar...
- Meta (4/18) Jun 25 2014 If you want something like a hash table that preserves insertion
- seany (3/7) Jun 26 2014 Thank you, this is _exactly_ what I was looking for! Thank you so
- Rene Zwanenburg (9/10) Jun 26 2014 Or import std.array to get the range primitives for slices:
- Meta (3/11) Jun 26 2014 Keep in mind that it will be up to you to ensure that each value
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
Aso, I wanted to mention that I did not find much info in the manual page on this.
Jun 25 2014
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
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
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
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
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
On Thursday, 26 June 2014 at 09:21:28 UTC, seany wrote:On Wednesday, 25 June 2014 at 14:17:50 UTC, Meta wrote:Keep in mind that it will be up to you to ensure that each value only exists in the array once.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