Hi guys, I'm just starting to play with Nim and encountered a following problem.
import re, strutils
proc findAllIndex(st, pat) : seq[int] =
var result = newSeq[int]()
var ind = st.find(pat)
while ind != -1:
result.add(ind)
ind = st.find(pat, ind+1)
var s = "testTest"
var t = "es"
echo findAllIndex(s, t)
when I executed it in aporia, there's an error:
> /tmp/aporia/a1 Traceback (most recent call last) a1.nim(13) a1 system.nim(1979) $ system.nim(2987) collectionToString SIGSEGV: Illegal storage access. (Try to compile with -d:useSysAssert -d:useGcAssert for details.)
I was thinking this might not be a problem of re itself, but of the way I handle the function. But I couldn't figure out what is wrong here. Can anyone help me with this? Thanks!
You are hiding the implicit result variable by declaring a new variable, result. Try:
import re, strutils
proc findAllIndex(st, pat) : seq[int] =
result = newSeq[int]()
var ind = st.find(pat)
while ind != -1:
result.add(ind)
ind = st.find(pat, ind+1)
var s = "testTest"
var t = "es"
echo findAllIndex(s, t)
@will : Thanks!
A related question here:
how is importing strutils makes the variable t acceptable by find, which is supposed to take an argument of Regex type?
how is importing strutils makes the variable t acceptable by find, which is supposed to take an argument of Regex type?
You're using strutils' find proc by supplying a string instead of a regex: http://nim-lang.org/strutils.html#find,string,string,int
@def
I see, thanks a lot!
strutils also defines a find procedure: http://nim-lang.org/strutils.html#find,string,string,int
if you want to be explicit you could qualify it with:
re.find(st, pat)
Edit: too late again.. :D
@will Thanks!
Thanks! I was just thinking about how to be explicit, or do a qualified import. So this time you answered in advance lol.