Post by bplus on Feb 22, 2022 11:57:34 GMT -5
Word$() is excellent tool for turning strings into Dynamic Arrays.
Without ReDim Preserve you can add, insert and delete items into word string that you can't do with regular arrays plus strings are way easier to pass to procedures or output from a Function.
You can even sort strings without arrays!
Without ReDim Preserve you can add, insert and delete items into word string that you can't do with regular arrays plus strings are way easier to pass to procedures or output from a Function.
You can even sort strings without arrays!
nItems = 25
'make up an word "array" to sort
Print "Unsorted:"
for i = 1 to nItems
r = int(rnd(1) * 10) + 1
a$ = ""
for j = 1 to r
r2 = int(rnd(1) * 26) + 1
a$ = a$;mid$("ABCDEFGHIJKLMNOPQRSTUVWXYZ", r2, 1)
next
print a$
b$ = b$;" ";a$
next
sorted$ = Sort$(b$)
locate 15, 1 : print "Sorted:"
for i = 1 to nItems
locate 15, i+ 1: print word$(sorted$, i)
next
' Word$() Tools - assuming no empty strings in word string delimited by spaces
' what is the upper bound of our string array? This returns the amount of elements
function wCnt(s$) 'of default space delimited string
while word$(s$, wc + 1) <> "" : wc = wc + 1 : wend
wCnt = wc
end function
sub wPut byref s$, p, put$ 'insert put$ in s$ as p word
wc = wCnt(s$)
for i = 1 to wc
if i = p then b$ = b$;put$;" "
b$ = b$;word$(s$, i);" "
next
s$ = b$
end sub
function Sort$(Me$) ' Me$ is a word string separated/delimited by single space, default for Word$()
nItems = wCnt(Me$)
for i = 1 to nItems
wd$ = word$(Me$, i)
if i = 1 then
rtn$ = wd$
else
found = 0
for j = 1 to i - 1
wd2$ = word$(rtn$, j)
if wd$ < wd2$ then
call wPut rtn$, j, wd$
found = 1
exit for
end if
next
if found = 0 then rtn$ = rtn$;" ";wd$
end if
next
Sort$ = rtn$
end function