Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Experimenting with a "StringList" type for simpler handling of string arrays/lists
#14
@Heimdall Perhaps I should not enable a phobic but AP's post above reminded me there is a workaround for 1 dim string arrays AKA Lists and that's "long strings" where each item is seperate by a delimter in a very long "list" of string items that can be "indexed" just like in arrays.

Here is an example demo, short and sweet you don't have to go to gethub to get:
Code: (Select All)
_Title "Word tools Demo" 'from: Word tools 2018-03-23.bas"  'B+ 2019-04-15

test$ = " 1   2 3  4  5  6  7  8  9  10   11 12 13 14 15   16 "
Print "Test string: *"; test$; "*"
test$ = wPrep$(test$)
Print: Print "Test string run through wPrep$() to remove excess spaces:"
Print "*"; test$; "*"
Print: Print "Test string has"; wCnt(test$); "'words' in it."
Print: Print "Show every third word in test string:"
For i = 3 To 16 Step 3
    Print "wrd$(test$,"; _Trim$(Str$(i)); ") = "; Wrd$(test$, i)
Next
Print: Print "What multiples of 5 are in test string?"
For i = 5 To 20 Step 5
    If wIn(test$, _Trim$(Str$(i))) > 0 Then Print i; "is in test string." Else Print i; "is NOT in test string."
Next
Print: Print "Substitute the phrase 'and all the rest...' for words after 10:"
Print wSubst$(test$, 11, wCnt(test$), "and all the rest...")


'return trimmed  source string s with one space between each word
Function wPrep$ (ss$)

    s$ = LTrim$(RTrim$(ss$))
    If Len(s$) = 0 Then wPrep$ = "": Exit Function
    'remove all double or more spaces
    p = InStr(s$, "  ")
    While p > 0
        s$ = Mid$(s$, 1, p) + Mid$(s$, p + 2, Len(s$) - p - 1)
        p = InStr(s$, "  ")
    Wend
    b$ = ""
    For i = 1 To Len(s$)
        c$ = Mid$(s$, i, 1)
        If Asc(c$) > 31 Then b$ = b$ + c$
    Next
    wPrep$ = b$
End Function

' This duplicates JB word(string, wordNumber) base 1, space as default delimiter
' by returning the Nth word of source string s
' this function assumes s has been through wPrep
Function Wrd$ (ss$, wNumber)
    's$ = wPrep(ss$)
    s$ = ss$ 'don't change ss$
    If Len(s$) = 0 Then Wrd$ = "": Exit Function
    w$ = "": c = 1
    For i = 1 To Len(s$)
        If Mid$(s$, i, 1) = " " Then
            If c = wNumber Then Wrd$ = w$: Exit Function
            w$ = "": c = c + 1
        Else
            w$ = w$ + Mid$(s$, i, 1)
        End If
    Next
    If c <> wNumber Then Wrd$ = " " Else Wrd$ = w$
End Function

'This function counts the words in source string s
'this function assumes s has been thru wPrep
Function wCnt (s$)
    Dim c As Integer, p As Integer, ip As Integer
    's = wPrep(s)
    If Len(s$) = 0 Then wCnt = 0: Exit Function
    c = 1: p = 1: ip = InStr(p, s$, " ")
    While ip
        c = c + 1: p = ip + 1: ip = InStr(p, s$, " ")
    Wend
    wCnt = c
End Function

'Where is word In source s, 0 = Not In source
'this function assumes s has been thru wPrep
Function wIn (s$, wd$)
    Dim wc As Integer, i As Integer
    wc = wCnt(s$): wIn = 0
    For i = 1 To wc
        If Wrd$(s$, i) = wd$ Then wIn = i: Exit Function
    Next
End Function

' substitute string in s to replace section first to last words inclusive
'this function assumes s has been thru wPrep
Function wSubst$ (s$, first, last, subst$)
    Dim wc As Integer, i As Integer, subF As Integer
    wc = wCnt(s$): b$ = ""
    For i = 1 To wc
        If first <= i And i <= last Then 'do this only once!
            If subF = 0 Then b$ = b$ + subst$ + " ": subF = 1
        Else
            b$ = b$ + Wrd$(s$, i) + " "
        End If
    Next
    wSubst$ = LTrim$(RTrim$(b$))
End Function

Now the above demo is set to work with a space as a delimter between each item so only words and numbers in string form work but it would be a piece of cake to use a comma for a delimiter so you can use spaces for phrases even sentences as long as commas not used in sentence. As AP says it's way slower than arrays would be but if you are really desparate it would be a fun exercise to practice coding skills and have something handy to show for your efforts.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Messages In This Thread
RE: Experimenting with a "StringList" type for simpler handling of string arrays/lists - by bplus - 12-18-2025, 12:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sub not Reconizing Dim as String pmackay 18 1,476 10-16-2025, 03:32 PM
Last Post: pmackay
  for performance, what's the best variable type to use for boolean _TRUE & _FALSE ? madscijr 12 1,256 09-29-2025, 02:59 PM
Last Post: dakra137
  Using modulo to loop through lists fistfullofnails 3 723 09-03-2025, 11:50 PM
Last Post: fistfullofnails
  Loading from file into _MEM? and LEN a TYPE... Unseen Machine 9 976 08-03-2025, 02:55 AM
Last Post: SMcNeill
  Illegal string-number conversion Herve 7 774 07-07-2025, 09:53 AM
Last Post: a740g

Forum Jump:


Users browsing this thread: 1 Guest(s)