This might get you started:
    
    
    import std/macros
    
    macro toLets[T](vals: openArray[T], vars: varargs[untyped]): untyped =
      result = newStmtList()
      for i, v in vars: # change| to const or var
        result.add(quote do: (let `v` = `vals`[`i`]))
    
    var fieldList = [1, 2, 3]
    fieldList.toLets a, b, c
    echo a, b, c
    
    
    Run

However, it may be even simpler to just define a bunch of names for your column 
indices and use that directly. E.g.,
    
    
    import std/macros
    
    macro mkNums(vars: varargs[untyped]): untyped =
      result = newStmtList()
      for i, v in vars:
        result.add(quote do: (const `v` = `i`))
    
    mkNums first, last, phone
    echo first, " ", last, " ", phone
    # echo field[last], ",", field[first]
    
    
    Run

Note that when processing CSV it is more efficient to not create a new 
`seq[string]` for each row, but rather iterate over the fields anyway (e.g. 
with the `strutils.split` _iterator_ called by `for` not the `proc`). 
[This](https://forum.nim-lang.org/t/9688) is the most recent thread on that 
topic, but it comes up a lot. So, I thought I might try to get ahead of it. ;-) 
If you run into trouble you might do some Forum searches.

You might also look at the code generated by my row processor 
[rp](https://github.com/c-blake/bu/blob/main/doc/rp.md) which is a hybrid 
approach: reusing the same seq[] object for each row, but still having one so 
you can reference `s[last]` (though that assumes your input file has column 
headers). You could also preprocess all your data into pre-parsed numeric 
binary files if you need to operate over them many times as in 
[nio](https://github.com/c-blake/nio) or probably load it into a database and 
probably 3 ways I'm neglecting to mention. There are really many ways to skin 
these cats... :-) Hopefully some of this helps you.

Reply via email to