WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Arrays
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ForEach in Array Trick

 Keywords: ForEach Tab delimited list array element loop 

Question:

The new ElseIf statement makes me pine again for the ForEach statement like this that deals with lists and array elements.
ForEach variable In list By delimiter

Answer:

One trick would be to convert your list to an array the loop through the elements in the array using ForEach.
strList = "a,b,c,d,e,f"
; Convert list to an array
arrA = ObjectType ("ARRAY", Arrayize (strList, ","))
intElements = ArrInfo (arrA, 1)
If intElements
   intItem = 0
   ; Access each element in the array
   ForEach arrElement In arrA
      intItem = intItem + 1
      Message (intItem, arrElement)
   Next
EndIf
Exit
... or the short version ...
; Short Version.
ForEach strItem In ObjectType ("ARRAY", Arrayize ("a,b,c,d,e,f", ","))
   Message ("",strItem)
Next
Exit
Here is an example that uses a UDF to convert the list to an array object.
#DefineSubRoutine udsListToVTArray (strList, strDelimiter)
Return ObjectType ("ARRAY", Arrayize (strList, strDelimiter))
#EndSubRoutine

strListColors = "red,green,blue"
arrColors = udsListToVTArray (strListColors, ",")
ForEach strColor In arrColors
   Pause ("Color Collection", strColor)
Next
Exit

Article ID:   W17662
Filename:   ForEach in Array Trick.txt
File Created: 2012:02:27:09:41:44
Last Updated: 2012:02:27:09:41:44