Hi - new to Nim and working through a program to massage a large file of data represented by the type
type TSegment* = object
ignore: bool
start: DateTime
finish: DateTime
duration: Duration
sequence: int
code: char
data: string
token: string
date: string
deductions: seq[int]
I then create a sequence of that type
var segment: TSegment
var segments = newSeqofCap[TSegment](1)
and add a bunch of segments to it.
segments.add(segment)
So now I have a mutable sequence of TSegments, which can be accessed as segments[x]. How do I initialize and add to segment[x].deductions? I've tried:
newSeqofCap[int](1)
but no luck.
Thanks!
@MaineTim: Yup, new to Nim...
In the following, I've shortened your code just to show the essentials:
type
TSeg = object
sub: seq[int]
var seg = TSeg(sub: @[])
var segments = newSeq[TSeg]()
for i in 0 .. 2: segments.add seg
segments.echo
segments[2].sub.add(7)
segments.add(TSeg(sub: @[42]))
segments.echo
No need to create the seq's using capacities (unless you are optimizing), creating all the elements from your uninitialized model of segment doesn't actually create any sub sequences as the default values are all zeros, including the deductions sub field, so you can't assign or add to it until it has been created with some sort of a new. In my example code, I initialized the model seg including initializing its seq contents with seq literal initializer as in @[] but you could use any sort of newSeq or newSeqOfCap, then either assign to the existing elements or add to the elements as you wish as I have done here.