I have several items formatted like this in a single string > "string1$ ---- string2$ ---- string3$ ----" .. etc...
I've been playing around with it and I always end up with "string1$ ---- string2$ ---- string3$ ----" "string1$ ---- string2$ ---- string3$ ----" in each list or array or map element when it should be that each "string*$" item in its own separate element in the list or array or map.
How do I put them (without the "----") in a List Or Array Or Map (If one of these is appropriate) as seperate items so I can retrieve each "string*$" item selectively?
Thank You
List or Array or Map - how do I ..
List or Array or Map - how do I ..
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
Re: List or Array or Map - how do I ..
Try something like this to split the string into an array - the code for lists or maps should be similar:
Code: Select all
Define.s str = "string1$ ---- string2$ ---- string3$ ----"
str = Left(str, Len(str) - 5)
Define.s sep = " ---- "
Define.i count = CountString(str, sep)
Define Dim arr.s(count)
Define i.i
For i = 0 To count
arr(i) = StringField(str, i + 1, sep)
Debug arr(i)
Next
Re: List or Array or Map - how do I ..
Something like this:smacker wrote:...items formatted like this in a single string > "string1$ ---- string2$ ---- string3$ ----"
How do I put them (without the "----") in a List Or Array Or Map ... so I can retrieve each "string*$" item selectively?
Code: Select all
Procedure split(strArray.s, delimiter.s, Array splitArray.s(1))
For i = 1 To CountString(strArray, delimiter) + 1
splitStr$ = Trim(StringField(strArray, i, delimiter))
If splitStr$ <> ""
ReDim splitArray(arrCount)
splitArray(arrCount) = splitStr$
arrCount + 1
EndIf
Next
EndProcedure
Procedure makeList(List new_List.s(), Array splitArray.s(1))
For i = 0 To ArraySize(splitArray())
AddElement(new_List())
new_List() = splitArray(i)
Next i
EndProcedure
Procedure makeMap(Map new_Map.s(), Array splitArray.s(1))
For i = 0 To ArraySize(splitArray())
new_Map(Str(i + 1)) = splitArray(i)
Next i
EndProcedure
;- into an array
Dim new_Array.s(0)
; the split() function will handle joined and redundant delimiters
strArray$ = "----string1$---- string2$ ----string3$ ---- ----" +
"string4$ ---- string5$----string6$ ----string7$"
split(strArray$, "----", new_Array())
Debug "values in new_Array:"
For i = 0 To ArraySize(new_Array())
Debug new_Array(i)
Next i
Debug "second item in array: " + new_Array(1)
;- into a list
NewList new_List.s()
makeList(new_List.s(), new_Array())
Debug #CR$ + "values in new_List:"
ForEach new_List()
Debug new_List()
Next
SelectElement(new_List(), 3)
Debug "fourth item in list: " + new_List()
;- into a map
NewMap new_Map.s()
makeMap(new_Map.s(), new_Array())
Debug #CR$ + "values in new_Map:"
ForEach new_Map()
Debug new_Map()
Next
Debug "sixth item in map: " + new_Map("6")
;- into another array with a different delimiter
Dim next_Array.s(0)
strArray$ = "stringA$..stringB$..stringC$..stringD$"
split(strArray$, "..", next_Array())
Debug #CR$ + "values in next_Array:"
For i = 0 To ArraySize(next_Array())
Debug next_Array(i)
Next i
Debug "first item in array: " + next_Array(0)
Hope it helps.

Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: List or Array or Map - how do I ..
Ok, got it, thanks for the replies and code snippets 

The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.