digitalmars.D.learn - Initialize multi dimensional associative dynamic array
- js.mdnq (23/23) Dec 02 2012 How does one initialize an array defined as
- bearophile (13/19) Dec 02 2012 Another option is to use an associative array where the keys are
- bearophile (14/20) Dec 02 2012 Another option is to use an associative array where the keys are
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (26/48) Dec 02 2012 Let me ask a related question: The following initialization does not
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/10) Dec 02 2012 Of course, more dynamically:
How does one initialize an array defined as A[B][C] arr; dynamically? (A,B,C are types, for example, int[int][string]) I want to store an array, indexed by strings, of ints, indexed by ints. For example, What I want is a hash map that maps integers to integers so I can do something like myval = arr[3243]; // has O(1) lookup But then I want to be able to extend this to use strings to subgroup the arrays: myval1 = arr["Group1"][3243]; // has O(1) lookup myval2 = arr["Group2"][3243]; // has O(1) lookup so my arr definition is int[int][string] arr; Or, another way to see it, is I want the Key's to be strings and the values to be int[int]. But when I try to access the value of the value I get an exception, I believe, because I haven't initialized the value. (because if I do a simple assign to the value it then works, but I'm trying to check if the value exists in the first place) I've also tried playing around with something like int[string[int]] and reversing the order(IIRC the order has to be backwards in the definition).
Dec 02 2012
js.mdnq:myval1 = arr["Group1"][3243]; // has O(1) lookup myval2 = arr["Group2"][3243]; // has O(1) lookupAnother option is to use an associative array where the keys are Tuple!(string, int): alias Tuple!(string, int) Tkey; int[Tkey] arr; myval1 = arr[Tkey("Group1", 3243)]; myval2 = arr[Tkey("Group2", 3243)]; But also take this in account: http://d.puremagic.com/issues/show_bug.cgi?id=3789But when I try to access the value of the value I get an exception, I believe, because I haven't initialized the value. (because if I do a simple assign to the value it then works, but I'm trying to check if the value exists in the first place)Why don't you show what you are trying to do with a little program, plus the errors you get? Bye, bearophile
Dec 02 2012
(Maybe this will arrive duplicated, thanks to the forum software) js.mdnq:myval1 = arr["Group1"][3243]; // has O(1) lookup myval2 = arr["Group2"][3243]; // has O(1) lookupAnother option is to use an associative array where the keys are Tuple!(string, int): alias Tuple!(string, int) Tkey; int[Tkey] arr; myval1 = arr[Tkey("Group1", 3243)]; myval2 = arr[Tkey("Group2", 3243)]; But also take this in account: http://d.puremagic.com/issues/show_bug.cgi?id=3789But when I try to access the value of the value I get an exception, I believe, because I haven't initialized the value. (because if I do a simple assign to the value it then works, but I'm trying to check if the value exists in the first place)Why don't you show what you are trying to do with a little program, plus the errors you get? Bye, bearophile
Dec 02 2012
On 12/02/2012 06:58 AM, js.mdnq wrote:How does one initialize an array defined as A[B][C] arr; dynamically? (A,B,C are types, for example, int[int][string])Let me ask a related question: The following initialization does not work. Am I doing something wrong? int[int][string] arr = [ "hello" : [ 1 : 100, 2 : 200 ] ]; Error: not an associative array initializerI want to store an array, indexed by strings, of ints, indexed by ints. For example, What I want is a hash map that maps integers to integers so I can do something like myval = arr[3243]; // has O(1) lookup But then I want to be able to extend this to use strings to subgroup the arrays: myval1 = arr["Group1"][3243]; // has O(1) lookup myval2 = arr["Group2"][3243]; // has O(1) lookup so my arr definition is int[int][string] arr; Or, another way to see it, is I want the Key's to be strings and the values to be int[int]. But when I try to access the value of the value I get an exception, I believe, because I haven't initialized the value.Allow me to repeat what bearophile said: It is very helpful if you show such problems in code. Even knowing the type of the exception is very helpful. Thanks.(because if I do a simple assign to the value it then works, but I'm trying to check if the value exists in the first place) I've also tried playing around with something like int[string[int]] and reversing the order(IIRC the order has to be backwards in thedefinition). This works for me: void main() { int[int][string] arr; arr["hello"] = [ 1 : 100, 2 : 200 ]; assert("world" !in arr); auto hello = "hello" in arr; // Note that 'in' produces a pointer: assert(typeid(hello) is typeid(int[int]*)); assert(hello); assert(1 in *hello); assert(2 in *hello); assert(3 !in *hello); } Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Dec 02 2012
On 12/02/2012 11:27 AM, Ali Çehreli wrote:int[int][string] arr; arr["hello"] = [ 1 : 100, 2 : 200 ];Of course, more dynamically: int[int][string] arr; int[int] a; a[1] = 100; a[2] = 200; arr["hello"] = a; Ali
Dec 02 2012