Page 1 of 1
List or Array or Map - how do I ..
Posted: Fri Sep 02, 2016 10:48 am
by smacker
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
Re: List or Array or Map - how do I ..
Posted: Fri Sep 02, 2016 12:07 pm
by PureFox
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 ..
Posted: Fri Sep 02, 2016 12:49 pm
by TI-994A
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?
Something like this:
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)
The
split() function would split and store the string elements into an array based on the given delimiter, and from there, it could be used directly, or assigned to a list or map if so desired. The
makeList() and
makeMap() procedures are simply convenience functions to assign values in an array into a map or list.
Hope it helps.

Re: List or Array or Map - how do I ..
Posted: Fri Sep 02, 2016 4:52 pm
by smacker
Ok, got it, thanks for the replies and code snippets
