Assosiative array pop

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 25 07:17:48 PDT 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..$]'.


More information about the Digitalmars-d-learn mailing list