'Use of declaration and/or definition array of tuple
How to have and declare/define array of tuple[ string , seq[ array[2,string] ] ]
Illustration is
var
a : seq[tuple[ string , seq[array[ 2, string]] ] ]
a[0] = [ "foo", [ ["hello", "foo"], ["foo", "bar" ] ] ]
a[1] = [ "bar", [ ["hello", "world"], ["hello", bar"] ] ]
a[2] = [ "hello", [ ["foo","bar"], ["hello","world"], ["bar", "baz"] ] ]
.
.
.
a[n] =
Tried rather long in this variations, e.g.
a = newSeqOfCap[ (string, newSeqOfCap[ array[ 2, string]](40) ) ](9)
all just end with no avail
Please guide the correct way. Thanks in advance for sincere help
Solution 1:[1]
You need to use newSeq, not newSeqOfCap. The latter only allocates the space for the sequence, not actually put anything in there for you to modify.
var a = newSeq[(string, seq[array[2, string]])](n)
a[0] = ("foo", @[["hello", "foo"], ["foo", "bar"]])
a[1] = ("bar", @[["hello", "world"], ["hello", "bar"]])
a[2] = ("hello", @[["foo", "bar"], ["hello", "world"], ["bar", "baz"]])
.
.
.
a[n] = ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | huantian |
