Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
June 25, 2014 Assosiative array pop | ||||
---|---|---|---|---|
| ||||
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. |
June 25, 2014 Re: Assosiative array pop | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | Aso, I wanted to mention that I did not find much info in the manual page on this. |
June 25, 2014 Re: Assosiative array pop | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | 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
|
June 25, 2014 Re: Assosiative array pop | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | 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
|
June 25, 2014 Re: Assosiative array pop | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | 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..$]'.
|
June 26, 2014 Re: Assosiative array pop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | 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!
|
June 26, 2014 Re: Assosiative array pop | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | 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);
}
|
June 26, 2014 Re: Assosiative array pop | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | 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.
|
Copyright © 1999-2021 by the D Language Foundation