I have a situation where I will have a long string of characters but where I'm inputting it, it can only handle 5 characters at a time. I will need to enter the entire string, but can't do so all at once, I have to do it 5 at a time.
So I need to split the string every 5 characters. My thought was to do a regexsplit on it, and make a list every 5 characters, then loop it to cycle through the entry but when I do the Regexsplit, it is giving me blank list entries as well and I can't figure out why. What's my problem here? Bad Regex? misunderstanding how splitregex works?
{formparagraph: name=datainput}
{=splitregex(datainput, "(.{5})")}
When I enter: 1234567890
It outputs this:
["", "12345", "", "67890", ""]
Hey @Kevin_Roberts here's one way to do it:
{formtext: name=datainput; default=1234567890}
{items= ceil(len(datainput)/5)}
{=[substring(datainput, (i-1)*5 + 1, 5) for i in seq(1,items)]}
1 Like
Cool. This works like I asked for. Upon further review I prefer the more elegant option that you mentioned @Gaurang_Tandon had on another thread where it will cut but do so at word breaks. I'm putting it here in case anyone else wants to do that one instead. For clarity, this one is split at 15 characters, not the 5.
{note}
{formparagraph: name=datainput; default=this is a test of the something something system; rows=10; cols=50}
{splitter=(string, max_chunk_size) -> block
var chunks = [ ] # our final output
var words = split(string, " ") # get individual words from the string
var current_chunk = "" # our current chunk
for index in seq(1, count(words))
var word = words[index]
var prepend = " " if index > 1 else ""
var new_chunk = current_chunk & prepend & word
if len(new_chunk) > max_chunk_size
if len(current_chunk) > 0
chunks = merge(chunks, [current_chunk])
endif
current_chunk = word
else
current_chunk = new_chunk
endif
endfor
if len(current_chunk) > 0
chunks = merge(chunks, [current_chunk])
endif
return chunks
endblock}
{endnote}{repeat: for item in splitter(datainput,15)}{if: item <>""}{=item}{key: enter}{wait: delay=+.8s}{endif}{endrepeat}
1 Like