Post by R.Wieser
Is there, and what is the format of, a "for each" loop which returns TWO
results (key and its associated value) at the same time. In PHP I would
use " foreach ($aList as $sKey => $sValue)". Is there something equivalent
in/under VBScript ?
Remark: I'm not looking for the dictionary object. I know that one. It
does not have it either. :-)
(Full disclosure: I'm implementing a IEnumVariant object)
' treat a 2D matrix as a key:value dictionary for ForEach
Dim ab() : ReDim ab(1,5) ' 2 columns, n rows or (0-Key 1-Value, entry pair 0..n)
' Redim Preserve only changes last index which lets us shrink/grow rows.
' key : value
ab(0,0) = "(0,0)" : ab(1,0) = "(1,0)"
ab(0,1) = "(0,1)" : ab(1,1) = "(1,1)"
ab(0,2) = "(0,2)" : ab(1,2) = "(1,2)"
ab(0,3) = "(0,3)" : ab(1,3) = "(1,3)"
ab(0,4) = "(0,4)" : ab(1,4) = "(1,4)"
ab(0,5) = "(0,5)" : ab(1,5) = "(1,5)"
Dim I, myKey, myValue : I = 0
For Each v In ab
WScript.Echo "Entry: ", I , v , " should be at (", I Mod 2 , "," , I \ 2, ")"
' you have to foreach loop twice to retrieve a key & matching value in order.
Select Case (I Mod 2)
Case 0: myKey = v ' v is key, loop again for its value.
Case 1: myValue = v ' v is value, got key & value now, process it.
' oDict.Add myKey , myValue do whatever you like here.
WScript.Echo "Entry: ", I , " processing" , myKey , myValue
I = I + 1
You could also create a small class of many fields and assign new instances of that to the ab(1,0..n) value array elements to get back multiple data items but it would be easier to increase the first index and the MOD call so they become new data columns you loop over before hitting up the next data row (2nd index).
In PHP and Dot.Net Dictionaries you're really iterating over key value pair objects and your PHP syntax is just telling it 2 names to map its contents to.
In script its how the basic IEnum interface is setup to work one value or object pointer. Arrays ForEach over single values in complete dimensions as defined left to right and that Scripting Dictionary object ForEach's over its set of keys in the order they were added. You can instance a Dot.Net dictionary through COM but you can't For Each iterate its Key/Value pairs. Other COM instanced Dot.Net collections (ArrayLists, Stacks, Queues) can be ForEach iterated because they are sets of single values or object pointers.