digitalmars.D.learn - array question
- seany (43/43) Dec 08 2013 consider the follwoing:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/27) Dec 08 2013 I think you wanted to append:
- seany (4/41) Dec 08 2013 no i wanted to set hashElement[hashesArr[j]] = plainArr[i][j];
- seany (11/11) Dec 09 2013 yet another array question :
- =?UTF-8?B?U2ltZW4gS2rDpnLDpXM=?= (13/24) Dec 09 2013 import std.range;
consider the follwoing: import tango.io.Stdout, tango.io.Path, tango.text.Util; import std.algorithm, std.string , std.stdio, std.array, std.conv, std.regex, std.typecons; //i know al imports are not necessary for this example, just ^c^v from my actual code alias string[] surSegments void makeHashmap(T,R)(T[] plainArr, string hashes, out R[] hashMap) { //first split the hashes string [] hashesArr = std.algorithm.splitter(hashes, ',').array; for(int i = 0; i < plainArr.length; i++) { R hashElement; for(int j = 0; j < hashesArr.length; j++) { hashElement[hashesArr[j]] = = plainArr[i][j]; } hashMap ~= hashElement; } } void main() { surSegments[] s = [[a,b,c,d], [e,f,g,h]]; h = "1,2,3,4"; surSegments[string][] ss; makeHashmap(s, h, ss); } i expect ss to look like : [ [1 => a, 2 => b, 3 => c, 4 => d], [1 => e, 2 => f, 3 => g, 4 => h] ] etc. instead i get compilation error: Error: cannot implicitly convert expression (hashesArr[cast(ulong)j]) of type string to string[] isn't hashElement of type surSegment[string] and hashElement[somestring] of type string, just like plainArr[i][j] ??
Dec 08 2013
On 12/08/2013 03:51 AM, seany wrote:consider the follwoing: import tango.io.Stdout, tango.io.Path, tango.text.Util; import std.algorithm, std.string , std.stdio, std.array, std.conv, std.regex, std.typecons; //i know al imports are not necessary for this example, just ^c^v from my actual code alias string[] surSegmentsThere are the missing semicolon above and other problems with the code.void makeHashmap(T,R)(T[] plainArr, string hashes, out R[] hashMap) { //first split the hashes string [] hashesArr = std.algorithm.splitter(hashes, ',').array; for(int i = 0; i < plainArr.length; i++) { R hashElement; for(int j = 0; j < hashesArr.length; j++) { hashElement[hashesArr[j]] = = plainArr[i][j];I think you wanted to append: hashElement[hashesArr[j]] ~= plainArr[i][j]; What helped me see what was going on was a bunch of pragma(msg) lines: pragma(msg, typeof(hashElement)); pragma(msg, typeof(hashesArr[j])); pragma(msg, typeof(hashElement[hashesArr[j]])); pragma(msg, typeof(plainArr[i][j])); Ali
Dec 08 2013
On Sunday, 8 December 2013 at 13:47:43 UTC, Ali Çehreli wrote:On 12/08/2013 03:51 AM, seany wrote:no i wanted to set hashElement[hashesArr[j]] = plainArr[i][j]; I realise that it had become string[][string][] sorry my badconsider the follwoing: import tango.io.Stdout, tango.io.Path, tango.text.Util; import std.algorithm, std.string , std.stdio, std.array,std.conv,std.regex, std.typecons; //i know al imports are not necessary for this example, just^c^v frommy actual code alias string[] surSegmentsThere are the missing semicolon above and other problems with the code.void makeHashmap(T,R)(T[] plainArr, string hashes, out R[]hashMap){ //first split the hashes string [] hashesArr = std.algorithm.splitter(hashes,',').array;for(int i = 0; i < plainArr.length; i++) { R hashElement; for(int j = 0; j < hashesArr.length; j++) { hashElement[hashesArr[j]] = = plainArr[i][j];I think you wanted to append: hashElement[hashesArr[j]] ~= plainArr[i][j]; What helped me see what was going on was a bunch of pragma(msg) lines: pragma(msg, typeof(hashElement)); pragma(msg, typeof(hashesArr[j])); pragma(msg, typeof(hashElement[hashesArr[j]])); pragma(msg, typeof(plainArr[i][j])); Ali
Dec 08 2013
yet another array question : I have defined : alias string[] surrealNum_segments; alias string[string] surrealNum_segments_withID; surrealNum_segments_withID BAR; Is there a built in function FOO, such that i could also write: FOO(surrealNum_segments) BAR; instead of surrealNum_segments_withID BAR; i.e. that will return an Associative Array type with string keys , with an array as argument?
Dec 09 2013
On 09.12.2013 23:03, seany wrote:yet another array question : I have defined : alias string[] surrealNum_segments; alias string[string] surrealNum_segments_withID; surrealNum_segments_withID BAR; Is there a built in function FOO, such that i could also write: FOO(surrealNum_segments) BAR; instead of surrealNum_segments_withID BAR; i.e. that will return an Associative Array type with string keys , with an array as argument?import std.range; alias string[] surrealNum_segments; alias ElementType!surrealNum_segments[string] surrealNum_segments_withID; Or, if you don't like writing all that (say, you have 1e8 array types you want ID versions of): template FOO(T : U[], U) { alias U[string] FOO; } alias FOO!surrealNum_segments surrealNum_segments_withID; But no, there is no built-in function of this kind. -- Simen
Dec 09 2013