in a code segment I have this array:
var str_ar:array[0..20,string]
str_ar[0] = @["10", "30", "40", "50", "80"]
If I loop over this array with for, i.e,
for m in str_ar[0]:
echo "=> " & m
I see this
=> @
=> [
=> "
=> 1
=> 0
=> "
=> ,
=>
=> "
=> 3
=> 0
=> "
=> ,
=>
=> "
=> 4
=> 0
etc. Why is this iterating over characters...I expected to see,
=>
=> "10"
=> "30"
=> "40"
etc. There is something fundamental I haven't caught on yet...what is it? Or is it something so simple, its just that its late in the night here ... If that is true, it may be a funny bug.
Your code should not compile, as you have an array of type string, and you assign to first element a whole seq of strings. That is invalid. Is this your full code, or have you defined converters which may convert sequences to strings in that way?
Maybe you are using latest Nim devel -- I heard that for Nim v 0.19 internal working of seq and strings is changed, so bug may occur from this. (I have still only stable v 0.18)
var str_seq = @["10", "30", "40", "50", "80"]
for m in str_seq:
echo "=> " & m
btw, which version of nim are you using? I'll get the error on your 2nd line:
Error: type mismatch: got <seq[string]> but expected 'string'
@stefan, thanks for your comment. It sets me thinking but I am not yet sure of the right way to proceed.
What I show on the right hand side of
var str_seq = @["10", "30", "40", "50", "80"]
is what I get from an echo of var str_seq. So this has more detail:
The actual code is like the following.
var js_fkvals = parseJson(fkvals)
echo "fkals = " & $jsfkvals
var js_fkvals_items:int = 0
for i in items(js_fkvals):
echo "i = " & $i
inc(js_fkvals_items)
var js_fkvals = parseJson(fkvals)
echo "fkals = " & $jsfkvals
var js_fkvals_items:int = 0
for i in items(js_fkvals):
echo "i = " & $i
inc(js_fkvals_items)
# ...
var str_ar:array[0..20,string]
var str_ar_inc:int = 0
for i in items(js_fkvals):
str_ar[str_ar_inc] = $i
inc(str_ar_inc)
echo "str_ar[0] " & str_ar[0]
echo "str_ar[1] " & str_ar[1]
The echo statements in the above code corrsponds to the following output when the code is run:
output
fkals = [[1,3,4,5,8],[10,30,40,50,80]]
i = [1,3,4,5,8]
i = [10,30,40,50,80]
jsfkval_items = 2
str_ar[0] [1,3,4,5,8]
str_ar[1] [10,30,40,50,80]
str_ar[str_ar_inc] = @["1", "3", "4", "5", "8"]
The last line here,
str_ar[str_ar_inc] = @["1", "3", "4", "5", "8"]
is what I am iterating over to get each string of the str_ar array.@mikra, I am running the development version I had installed last week,
nim -v
Nim Compiler Version 0.18.1 [Linux: amd64]
Compiled at 2018-07-19
Copyright (c) 2006-2018 by Andreas Rumpf
git hash: f92d61b1f4e193bd17e1f76722d26f1f0605ad17
active boot switches: -d:release
Hi @stefan, apologies that I did not catch on to your significant first line --
Your code should not compile, as you have an array of type string, and you assign to first element a whole seq of strings. That is invalid.
I will go back to this issue. The development version compiles and runs it anyway. The stable version breaks at an earlier parseJson point, where I again have an array containing strings from parseJson reading a valid json from the file system.Note that echo is not a really good command to see the type of variables:
import typetraits
var str_seq = @["10", "30", "40", "50", "80"]
echo str_seq
echo str_seq.type
var str_seq2 = """@["10", "30", "40", "50", "80"]"""
echo str_seq2
echo str_seq2.type
stefan@nuc /tmp/h $ ./t
@["10", "30", "40", "50", "80"]
seq[string]
@["10", "30", "40", "50", "80"]
string
So echo output looks the same for a seq and a string. typetraits.type shows you the real type of your vars. Generally, when in doubt, it may be better to explicitely write down types in var declarations, and use type inference only when one is absolutely sure about data types. I have never used parseJson(), so I do not really understand your example code, but I think that your vars are plain strings, and you iterate over that strings, so output is the chars of that strings.
Nim Compiler Version 0.18.1 [Windows: amd64]
Compiled at 2018-06-03
Copyright (c) 2006-2018 by Andreas Rumpf
git hash: 91765e583da91b8288e18e74d4196713dc2ede18
active boot switches: -d:release
indeed it looks like a strang bug (it seems you are iterating over the source representation of that line)
Hi @Stefan, you exactly nailed the problem -- I was working with a string type (obtained from converting a JsonNode to string.) This roundabout step sent me on the wild goose chase. There is no need to do this string conversion. I can just keep the JsonNode and then parse out any json component that I want with using the parseJson.
Thanks for all the related comments. Helps me learn Nim a bit more better.