If you don't want to allocate a seq (like you do when you invoke pairs), you
can use enumerate from
[https://nim-lang.org/docs/manual.html#macros-for-loop-macro](https://nim-lang.org/docs/manual.html#macros-for-loop-macro)
Aha: success
for myKey, myValue in readFile(day02PathAndName).split(',').pairs():
result[myKey.int64]=myValue.parseInt
Run
It might be more productive if I advise you that I spent several days perusing
the nim documentation before I admitted defeat and asked a question here.
@Solitude's comments highlighted something that is not adequately referenced
when you read the for loop section of the manual.
I'll have to wait until my knowledge of nim improves before I can that. In the
meantime I'm using
let mySeq:seq[string]=readFile(day02PathAndName).split(',')
for myKey, myValue in mySeq:
result[myKey.int64]=myValue.parseint.int64
Run
Seems to be a use-case for Nim's csv parser,
[https://nim-lang.org/docs/parsecsv.html](https://nim-lang.org/docs/parsecsv.html)
Sorry for being lazy earlier and for my poor English. What I meant was this:
import strutils
var
file = """
Hello,World
Key,Value
"""
for line in file.split("\n"): # iterator returns string
var data = line.split(",") # proc returns s
invoke items/pairs iterator explicitly
On further reading it seems there is a split proc and a split iterator. The
former returns a sequence of substrings. The code I have seems to be using the
iterator version. How would I indicate to nim to use the proc rather than the
iterator version of split?
there is no split iterator that returns two values at a time. you're confusing
it with sequence and implicit pairs. you have to keep track of the indexes on
your own here.
if that "split(',')" is from strutils, it returns seq[string], not tuple. I
guess you need to use something like this:
for line in readFile(day02PathAndName):
var data = line.split(',')
if data.len() == 2:
myKey = data[0]
myValue = data[1]
result.
10 matches
Mail list logo