Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 08, 2013 array question | ||||
---|---|---|---|---|
| ||||
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] ?? |
December 08, 2013 Re: array question | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | 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[] surSegments There 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 |
December 08, 2013 Re: array question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Sunday, 8 December 2013 at 13:47:43 UTC, Ali Çehreli wrote:
> 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[] surSegments
>
> There 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
no i wanted to set hashElement[hashesArr[j]] = plainArr[i][j];
I realise that it had become string[][string][]
sorry my bad
|
December 09, 2013 Re: array question | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | 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? |
December 09, 2013 Re: array question | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | 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 |
Copyright © 1999-2021 by the D Language Foundation