Discussion:
Return a key, value pair from a "for each" loop.
(too old to reply)
R.Wieser
2016-01-26 11:31:39 UTC
Permalink
Hello all,

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)

Regards,
Rudy Wieser
Evertjan.
2016-01-26 11:54:15 UTC
Permalink
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 ?
No.
This is about arrays!
There is no "Key" available, imho.

otoh,
you probably can deduce the member-number:

<%
Dim fruits(3), n
fruits(0) = "Orange"
fruits(1) = "Apple"
fruits(2) = "Mango"
n = 0
For Each fruit In fruits
Response.write n & ": " & fruit & "<br>"
n = n + 1
Next
%>

But why not use a counting for-next with a ubound()?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
R.Wieser
2016-01-26 13:18:02 UTC
Permalink
Evertjan,
Post by Evertjan.
No.
Ok, thanks.
Post by Evertjan.
This is about arrays!
True. However, the source of the data is, in my case, a two-dimensional
non-variant array, with the first element containing the ID (type) of data
stored in the second element. I need both, or the the time-and-effort to
implement a "for each" loop is not worth its troubles (I already have "by
index" access into the array).
Post by Evertjan.
There is no "Key" available, imho.
I'm not sure which "available" you mean here. My data certainly does have
it (hence my question). I must therefore assume you mean that VBScript does
not know/have it (repeating your first "No").
Post by Evertjan.
otoh,
:-) I can already access the fields by index and iterate over them with a
"for" loop (using GetFieldID and GetFieldData methods). I just thought that
I could simplify the whole thing (for whomever is going to use the object),
and at the same time create a full fledged object (allowing other scripting
environments not supporting "for" loops access to the data too).

Remark: I can ofcourse let the "for each" return a "field" object, which
than exposes "ID" and "Value" properties (maybe even an "Index" property too
:-) ), but that throws even more junk into my program.

On the other hand, I will probably implement it just to see if I can (get it
to work), and when it does remove it again. :-)

Regards,
Rudy Wieser
Post by Evertjan.
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 ?
No.
This is about arrays!
There is no "Key" available, imho.
otoh,
<%
Dim fruits(3), n
fruits(0) = "Orange"
fruits(1) = "Apple"
fruits(2) = "Mango"
n = 0
For Each fruit In fruits
Response.write n & ": " & fruit & "<br>"
n = n + 1
Next
%>
But why not use a counting for-next with a ubound()?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan.
2016-01-26 13:42:45 UTC
Permalink
Post by R.Wieser
True. However, the source of the data is, in my case, a two-dimensional
non-variant array, with the first element containing the ID (type) of data
stored in the second element. I need both, or the the time-and-effort to
implement a "for each" loop is not worth its troubles (I already have "by
index" access into the array).
But your ID is just data, not a key [object parlance] = placenumber [array].

I have no idea what "for each" will do in a two-dim array.

Since "for each" typically does not define any order,
the order being a by-product of the implementation,
I usually stay far away from it.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
R.Wieser
2016-01-26 14:27:45 UTC
Permalink
Evertjan,
Post by Evertjan.
But your ID is just data,
True.
Post by Evertjan.
not a key [object parlance] =
placenumber [array].
A key is definitily *not* the same as an index. Just take a look at a
dictionary object.
Post by Evertjan.
I have no idea what "for each" will do in a two-dim array.
As no two-result "for each" seems to exist, it will do nothing. :-)

But *if* such a two-result "for each" would exist you still would not know,
as what is put in those results is fully upto the object in which the
enumeration is implemented.

And as I'm the one implementing that enumeration object ....
Post by Evertjan.
Since "for each" typically does not define any order,
the order being a by-product of the implementation,
I usually stay far away from it.
The order of the data is not important, and even irellevant to me. I need
to be able to access or find the data by way of its ID. Much like the
dictionary object does. The only difference is that I cannot be absolutily
sure a certain ID does not appear more than once in the array (which
excludes the use of such a dictionary object)

Regards,
Rudy Wieser
Post by Evertjan.
Post by R.Wieser
True. However, the source of the data is, in my case, a two-dimensional
non-variant array, with the first element containing the ID (type) of data
stored in the second element. I need both, or the the time-and-effort to
implement a "for each" loop is not worth its troubles (I already have "by
index" access into the array).
But your ID is just data, not a key [object parlance] = placenumber [array].
I have no idea what "for each" will do in a two-dim array.
Since "for each" typically does not define any order,
the order being a by-product of the implementation,
I usually stay far away from it.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan.
2016-01-26 16:08:24 UTC
Permalink
Post by R.Wieser
Post by Evertjan.
not a key [object parlance] =
placenumber [array].
A key is definitily *not* the same as an index.
In the case af an array it is.
Post by R.Wieser
Just take a look at a dictionary object.
Ah, but that is not an array, and "for-each" in VBS is about arrays.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
R.Wieser
2016-01-26 17:44:32 UTC
Permalink
Evertjan,
Post by Evertjan.
In the case af an array it is.
Not even than. A "key" indicates a bit more than a by sheer luck created
relation between index(x) and data(y). To make an example: just insert
something in that array of yours, and see a number of your "keys" being
broken (pointing to the wrong/different data).

Besides the above, how is this relevant to the case I described ?
Post by Evertjan.
Ah, but that is not an array, and "for-each" in VBS is about arrays.
I'm afraid that you are mistaken there. "for each" is just a VBScript
method to address/access an enumerator object. Where that enumerator
object gets its data from is fully upto whomever implements the object.

Believe me, I've just spend a number of days looking at examples using
enumeration objects, and several have got little, if anything to do with an
array.

One of the examples, using an IEnumVariant object (which is the base class
of several others), walked thru a linked list. Not an array anywhere.

And please do me a favour and look up ' "dictionary object" "for each" '.
:-)

Regards,
Rudy Wieser
Post by Evertjan.
Post by R.Wieser
Post by Evertjan.
not a key [object parlance] =
placenumber [array].
A key is definitily *not* the same as an index.
In the case af an array it is.
Post by R.Wieser
Just take a look at a dictionary object.
Ah, but that is not an array, and "for-each" in VBS is about arrays.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mayayana
2016-01-26 13:26:52 UTC
Permalink
It might be easier if you described what you're
actually trying to accomplish.

It sounds like something I'd do with a class.
I often use classes instead of collections in VB
because collections are slow and limited. If I
need pairs I'd typically just use 2 arrays in a
class, then structure the methods and properties
to make the class function as a pair-storage
system.
R.Wieser
2016-01-26 15:41:36 UTC
Permalink
Mayayana,
Post by Mayayana
It might be easier if you described what you're
actually trying to accomplish.
I did. I need to know if there is a "for each" which returns two results,
comparable to how the "foreach" in PHP works.
Post by Mayayana
It sounds like something I'd do with a class.
As I mentioned to Evertjan I will probably explore that type of solution too
(using a "field" class as the single return value of a "for each"
enumeration). But I also am hobbyist enough to spend (some) time on
checking if a simpler implementation exists
Post by Mayayana
If I need pairs I'd typically just use 2 arrays in a
class, then structure the methods and properties
to make the class function as a pair-storage
system.
Already did that, by implementing the methods/propertygets
GetFieldID(FieldIndex) and GetFieldData(FieldIndex).

Enumeration methods and related objects is just something I also want to
explore, to further my understanding of those objects and how to access
them.

Regards,
Rudy Wieser
Post by Mayayana
It might be easier if you described what you're
actually trying to accomplish.
It sounds like something I'd do with a class.
I often use classes instead of collections in VB
because collections are slow and limited. If I
need pairs I'd typically just use 2 arrays in a
class, then structure the methods and properties
to make the class function as a pair-storage
system.
JJ
2016-01-26 18:01:46 UTC
Permalink
Post by R.Wieser
Hello all,
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 ?
No.

FYI, PHP array is not really an array. It's an object that works like an
array. Unlike C/C++ array which has no key.
R.Wieser
2016-01-26 19:41:55 UTC
Permalink
JJ,
Post by JJ
No.
OK, thanks.
Post by JJ
FYI, PHP array is not really an array.
Its not about the PHP command. I only gave that as an example what a
two-result "for each" command looks like in another language. In
VBScript I could imagine something like "for each variant1, variant2 in
object" (realized it to late, otherwise I would have given that one
instead).

Also, you can not "for each" a VBScript array either, only objects (where
those objects internally get their data from is a fully other matter), so in
that regard they are more alike a PHP "array" than than you might think ...
:-)

Regards,
Rudy Wieser
Post by JJ
Post by R.Wieser
Hello all,
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 ?
No.
FYI, PHP array is not really an array. It's an object that works like an
array. Unlike C/C++ array which has no key.
JJ
2016-01-27 09:56:45 UTC
Permalink
Post by R.Wieser
Its not about the PHP command. I only gave that as an example what a
two-result "for each" command looks like in another language. In
VBScript I could imagine something like "for each variant1, variant2 in
object" (realized it to late, otherwise I would have given that one
instead).
Oh, OK.
Post by R.Wieser
Also, you can not "for each" a VBScript array either, only objects (where
those objects internally get their data from is a fully other matter),
A VB array is actually an object. VBScript is simply a script version of VB,
so it's the same. So "for each" can enumerate VB array elements.

function print(arg)
s = ""
for each val in arg
s = s & val & ","
next
wscript.echo s
end function

dim arr1(2,1)
arr1(0,0) = 1
arr1(1,0) = 2
arr1(2,0) = 3
arr1(0,1) = 4
arr1(1,1) = 5
arr1(2,1) = 6
print(arr1)

dim arr2
arr2 = array(1,2,3,4,5,6)
print(arr2)
Post by R.Wieser
so in
that regard they are more alike a PHP "array" than than you might think ...
:-)
Yes, they're both an array object, not a primitive array. The difference is
that PHP array is keyed, while VB array isn't. A keyed array object can have
partial elements. e.g. has element #0 and #2 but not #1. In this case,
although the array length is 3, the array only has 2 elements and consume
memory for only 2 elements. Element #1 would be unassigned, empty or
undefined.
R.Wieser
2016-01-27 12:16:48 UTC
Permalink
JJ,
Post by JJ
A VB array is actually an object.
Your code shows two different types of them. Which one ? Or both ?
Post by JJ
So "for each" can enumerate VB array elements.
First of, I was not aware that a DIM-ed array could be "for each" -ed like
an array object. Thanks for that.

But the output of the "for each" -ed DIM-ed multi-dimension array is pretty
worthless for my purposes, as the column the data is is rather important
(the first column signifies the use of the second one). And that gets lost
in the conversion ...

Currently I've got this:

for i=0 to oRecord.FieldCount -1
wscript.echo oRecord.GetFieldID(i) &" "& oRecord.GetFieldData(i)
if oRecord.GetFieldID(i) = XXX then
' do something with oRecord.GetFieldData(i)
end if
next

But what I wanted to try to implement (and is answered as not possible) was
this:

for each ID,Data in oRecord
wscript.echo ID &" "& Data
if ID = XXX then
' do something with Data
end if
next

Regards,
Rudy Wieser
Post by JJ
Post by R.Wieser
Its not about the PHP command. I only gave that as an example what a
two-result "for each" command looks like in another language. In
VBScript I could imagine something like "for each variant1, variant2 in
object" (realized it to late, otherwise I would have given that one
instead).
Oh, OK.
Post by R.Wieser
Also, you can not "for each" a VBScript array either, only objects (where
those objects internally get their data from is a fully other matter),
A VB array is actually an object. VBScript is simply a script version of VB,
so it's the same. So "for each" can enumerate VB array elements.
function print(arg)
s = ""
for each val in arg
s = s & val & ","
next
wscript.echo s
end function
dim arr1(2,1)
arr1(0,0) = 1
arr1(1,0) = 2
arr1(2,0) = 3
arr1(0,1) = 4
arr1(1,1) = 5
arr1(2,1) = 6
print(arr1)
dim arr2
arr2 = array(1,2,3,4,5,6)
print(arr2)
Post by R.Wieser
so in
that regard they are more alike a PHP "array" than than you might think ...
:-)
Yes, they're both an array object, not a primitive array. The difference is
that PHP array is keyed, while VB array isn't. A keyed array object can have
partial elements. e.g. has element #0 and #2 but not #1. In this case,
although the array length is 3, the array only has 2 elements and consume
memory for only 2 elements. Element #1 would be unassigned, empty or
undefined.
JJ
2016-01-27 21:16:27 UTC
Permalink
Post by R.Wieser
Your code shows two different types of them. Which one ? Or both ?
Both.
Post by R.Wieser
But the output of the "for each" -ed DIM-ed multi-dimension array is pretty
worthless for my purposes, as the column the data is is rather important
(the first column signifies the use of the second one). And that gets lost
in the conversion ...
I see what you mean.
Post by R.Wieser
for i=0 to oRecord.FieldCount -1
wscript.echo oRecord.GetFieldID(i) &" "& oRecord.GetFieldData(i)
if oRecord.GetFieldID(i) = XXX then
' do something with oRecord.GetFieldData(i)
end if
next
I don't know from where exactly that oRecord is, but if it's a collection
(which is an array-like object), you should be able to use it on "for each"
to simplify your current code. e.g.

for each field in oRecord
wscript.echo field.ID &" "& field.Data
if field.ID = XXX then
' do something with field.Data
end if
next

Otherwise, oRecord is an object with a private array, and all access to the
array must come from oRecord's methods and properties. In this case, oRecord
array elements are non enumerable by standard means.
Post by R.Wieser
But what I wanted to try to implement (and is answered as not possible) was
for each ID,Data in oRecord
wscript.echo ID &" "& Data
if ID = XXX then
' do something with Data
end if
next
Unfortunately, it's not possible for VBScript. Not even VB.NET. It *is* a
basic programming language after all.
R.Wieser
2016-01-27 23:17:03 UTC
Permalink
JJ,
Post by JJ
Post by R.Wieser
Your code shows two different types of them.
Which one ? Or both ?
Both.
Ok. I was assuming that the DIM-med one wasn't one, as its build in.
Post by JJ
I don't know from where exactly that oRecord is,
I don't understand how that is important, but as you seem to think it is,
I'm retrieving it like this :

set oRecord = oDatabase.GetRecord(RecordIndex)

And yes, I'm also implementing/testing that oDatabase object.
Post by JJ
but if it's a collection (which is an array-like object)
It is exactly what I want (read: write) it to be. I, as its programmer,
have that power (can't think of a good TRON (the movie) reference here
though ...) :-)
Post by JJ
you should be able to use it on "for each" to simplify your
current code.
:-) Yes, I certainly can do that. And its exactly what I've been
mentioning a number of times thruout this thread as a possibility.

The reason I've not done that in the first place is because I wanted, if
possible, to keep the number of (sub)objects down. Less objects means less
to track and delete when I do not need them anymore.

Regards,
Rudy Wieser
Post by JJ
Post by R.Wieser
Your code shows two different types of them. Which one ? Or both ?
Both.
Post by R.Wieser
But the output of the "for each" -ed DIM-ed multi-dimension array is pretty
worthless for my purposes, as the column the data is is rather important
(the first column signifies the use of the second one). And that gets lost
in the conversion ...
I see what you mean.
Post by R.Wieser
for i=0 to oRecord.FieldCount -1
wscript.echo oRecord.GetFieldID(i) &" "& oRecord.GetFieldData(i)
if oRecord.GetFieldID(i) = XXX then
' do something with oRecord.GetFieldData(i)
end if
next
I don't know from where exactly that oRecord is, but if it's a collection
(which is an array-like object), you should be able to use it on "for each"
to simplify your current code. e.g.
for each field in oRecord
wscript.echo field.ID &" "& field.Data
if field.ID = XXX then
' do something with field.Data
end if
next
Otherwise, oRecord is an object with a private array, and all access to the
array must come from oRecord's methods and properties. In this case, oRecord
array elements are non enumerable by standard means.
Post by R.Wieser
But what I wanted to try to implement (and is answered as not possible) was
for each ID,Data in oRecord
wscript.echo ID &" "& Data
if ID = XXX then
' do something with Data
end if
next
Unfortunately, it's not possible for VBScript. Not even VB.NET. It *is* a
basic programming language after all.
Evertjan.
2016-01-27 23:22:20 UTC
Permalink
Post by JJ
Unfortunately, it's not possible for VBScript. Not even VB.NET. It *is* a
basic programming language after all.
"Beginners All-purpose Symbolic Instruction Code", nothing basic about BASIC.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
JJ
2016-01-28 09:03:41 UTC
Permalink
Post by Evertjan.
"Beginners All-purpose Symbolic Instruction Code", nothing basic about BASIC.
I assume you meant that it's self explanatory? Or do you mean that BASIC is
the most basic of all programming languages?
Evertjan.
2016-01-28 10:59:33 UTC
Permalink
Post by JJ
Post by Evertjan.
"Beginners All-purpose Symbolic Instruction Code", nothing basic about BASIC.
I assume you meant that it's self explanatory? Or do you mean that BASIC
is the most basic of all programming languages?
Please do not assume.

BASIC is just a name given to a programming language,
what the namegiver implied you should ask him or her.

afaisi, there is nothing inherent basic about BASIC,
but that in hindsight it has a long history,
it made programming more available to the beginner,
does not mean basic in the sense of rudimentary,
so in fact it was a "higher" language
than the "high" languages of that time.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
GS
2016-01-26 23:10:24 UTC
Permalink
Post by R.Wieser
Hello all,
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)
Regards,
Rudy Wieser
Here's how I access 2D arrays...

Dim the array for the number of rows by number of columns
Dim MyArray(0 To 9, 0 To 1)
'//results a 10 x 2 array fixed size array. (1st dim is fixed)

MyArray(0, 0) is row1, col1
MyArray(0, 1) is row1, col2
MyArray(1, 0) is row2, col1
MyArray(1, 1) is row2, col2
..and so on.

So...

For n = LBound(MyArray) To UBound(MyArray)
If MyArray(n, 0) = value0 Then MyArray(n, 1) = value1
Next 'n

..where element0 contains a pseudo 'key' I can associate to, and
element1 contains the value that pertains to the pseudo 'key'.
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2016-01-26 23:12:41 UTC
Permalink
sb...
..where Column0 contains a pseudo 'key' I can associate to, and
Column1 contains the value that pertains to the pseudo 'key'.
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
R.Wieser
2016-01-27 10:33:11 UTC
Permalink
Garry,
Post by GS
Here's how I access 2D arrays...
Thanks, but I've already got that part nailed.

Its the "for each" approach I'm now trying to accomplish something with, in
combination with an object I'm currently busy with coding.

Regards,
Rudy Wieser
Post by GS
Post by R.Wieser
Hello all,
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)
Regards,
Rudy Wieser
Here's how I access 2D arrays...
Dim the array for the number of rows by number of columns
Dim MyArray(0 To 9, 0 To 1)
'//results a 10 x 2 array fixed size array. (1st dim is fixed)
MyArray(0, 0) is row1, col1
MyArray(0, 1) is row1, col2
MyArray(1, 0) is row2, col1
MyArray(1, 1) is row2, col2
..and so on.
So...
For n = LBound(MyArray) To UBound(MyArray)
If MyArray(n, 0) = value0 Then MyArray(n, 1) = value1
Next 'n
..where element0 contains a pseudo 'key' I can associate to, and
element1 contains the value that pertains to the pseudo 'key'.
--
Garry
Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2016-01-27 17:27:48 UTC
Permalink
Post by R.Wieser
Its the "for each" approach I'm now trying to accomplish something with, in
combination with an object I'm currently busy with coding.
For..Each works with object 'items' just fine, but you say you want to
manage a 2D array and IMO For..Each isn't going to be efficient without
restructuring the data to work with it. That could result a convoluted
maintenance mess I don't care to manage and so expect you wouldn't
either.

For Each vItem in Object.Items...

is not the same as looping a vbs array.

Also, I most often find it easier to store value pairs like this...

key:value

..so I can use a 1D array to locate the key, then Split() on that index
to access the value via a For..Next loop. Given that most of my apps
use a spreadsheet, 2D arrays are a natural thing to work with.
Fortunately, the spreadsheet has an Index() function that allows me to
access an entire row or column of the array. Too bad vb or vbs doesn't
have such a function!<g>
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
R.Wieser
2016-01-27 18:42:04 UTC
Permalink
Garry,
Post by GS
For..Each works with object 'items' just fine,
With any kind of variant actually, even with ones VBScript does not even
know what to do with (like containing a VT_I8 ). :-)
Post by GS
but you say you want to manage a 2D array
Only in the very broadest sense. What I actually do is to access a set of
several ID -> value pairs. You could, for argument sake and temporarily,
regard it as a 2D array, but that is not what it is (well, maybe it is but
it should definitily *not* be the focus of attention :-) )
Post by GS
For Each vItem in Object.Items...
is not the same as looping a vbs array.
In what sense ? I see very little difference, especially when you compare
a one-dimension DIM-med array with an array object. Both are loopable by a
"for each" (as JJ has shown me), and will return the exact same data.
Post by GS
Also, I most often find it easier to store value pairs like this...
key:value
That is an acceptable approach for some home made scripting, but not when
its returned from an object. At least, not to me. But yes, on occasion
I've used similar tricks.

Also, there is absolutily no technical(!) problem with returning the ID ->
Value pair in a two-element simple array (I already have done so, just to
see if it works). Its just that I don't think that that solution is really
acceptable either (looks like shite to me). :-\

Than again, maybe I just want to much ... :-( :-)

Regards,
Rudy Wieser
Post by GS
Post by R.Wieser
Its the "for each" approach I'm now trying to accomplish something with, in
combination with an object I'm currently busy with coding.
For..Each works with object 'items' just fine, but you say you want to
manage a 2D array and IMO For..Each isn't going to be efficient without
restructuring the data to work with it. That could result a convoluted
maintenance mess I don't care to manage and so expect you wouldn't
either.
For Each vItem in Object.Items...
is not the same as looping a vbs array.
Also, I most often find it easier to store value pairs like this...
key:value
..so I can use a 1D array to locate the key, then Split() on that index
to access the value via a For..Next loop. Given that most of my apps
use a spreadsheet, 2D arrays are a natural thing to work with.
Fortunately, the spreadsheet has an Index() function that allows me to
access an entire row or column of the array. Too bad vb or vbs doesn't
have such a function!<g>
--
Garry
Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2016-01-27 19:36:18 UTC
Permalink
Post by R.Wieser
Post by GS
For Each vItem in Object.Items...
is not the same as looping a vbs array.
In what sense ? I see very little difference, especially when you compare
a one-dimension DIM-med array with an array object.
In the sense that looping an object allows direct access to the
property values of 'each' object.item, or its subitems. Looping an
array allows access to each index of the array 1D at a time.

So if your values were stored, let's say, in a 1D array of arrays then
you'd have to further loop each element's array to access the values.

In an object (ie: class as Mayayana suggests) tou can access the values
directly as properties...

Object.Items(n).Value1
Object.Items(n).Value2
Object.Items(n).Value3
..and so on

..where your code could be better self-documenting using good naming
practices for the properties, as well as easier maintenance, IMO!
Post by R.Wieser
Than again, maybe I just want to much ...
Nothing wrong with that!<g>
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
R.Wieser
2016-01-27 22:34:18 UTC
Permalink
GS,
Post by GS
In the sense that looping an object allows direct access to
the property values of 'each' object.item, or its subitems.
That fully depends on what that object returns (how its written). Looping
to both a DIM-med and an object array just returns the stored values,
nothing more.

Only in the case where that "looped object" returns *another* (sub) object
than yes, you can access that (sub) objects properties.
Post by GS
In an object (ie: class as Mayayana suggests) tou can access
the values directly as properties...
I'm not sure if you are aware, but you're accessing *two* objects per line
there: The "Object" one, and the "Object.Items(n)" one. Costly ...

But yes, thats pretty-much what I have too:

MainObject.GetRecord(RecordIndex).GetFieldID(FieldIndex)
MainObject.GetRecord(RecordIndex).GetFieldData(FieldIndex)

.. though I rather write that as:

set oRecord = MainObject.GetRecord(RecordIndex)
... oRecord.GetFieldID(FieldIndex)
... oRecord.GetFieldData(FieldIndex)
set oRecord = nothing

, as this retrieves the full record only once.
Post by GS
..where your code could be better self-documenting using
good naming practices for the properties, as well as easier
maintenance, IMO!
Yep, that was exactly what I want(ed) to happen/have. :-)

I think I will either end up with this:

for i=0 to oRecord.FieldCount-1
wscript.echo oRecord.GetFieldID(i) &" "& oRecord.GetFieldData(i)
next

or with this:

for each oField in oRecord
wscript.echo oField.ID &" "& oField.Data
next

The latter one looks best but internally costs two extra objects (the
enumerator and the field objects), while the former one does the same, but
is not as flexible -- a "for each" can easily internally skip records on the
run if needed (think filtering), a "for" loop needs the full count upfront,
so no sneaky dropping of certain entries.

Regards
Rudy Wieser
Post by GS
Post by R.Wieser
Post by GS
For Each vItem in Object.Items...
is not the same as looping a vbs array.
In what sense ? I see very little difference, especially when you compare
a one-dimension DIM-med array with an array object.
In the sense that looping an object allows direct access to the
property values of 'each' object.item, or its subitems. Looping an
array allows access to each index of the array 1D at a time.
So if your values were stored, let's say, in a 1D array of arrays then
you'd have to further loop each element's array to access the values.
In an object (ie: class as Mayayana suggests) tou can access the values
directly as properties...
Object.Items(n).Value1
Object.Items(n).Value2
Object.Items(n).Value3
..and so on
..where your code could be better self-documenting using good naming
practices for the properties, as well as easier maintenance, IMO!
Post by R.Wieser
Than again, maybe I just want to much ...
Nothing wrong with that!<g>
--
Garry
Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2016-01-27 23:13:01 UTC
Permalink
Post by R.Wieser
for i=0 to oRecord.FieldCount-1
wscript.echo oRecord.GetFieldID(i) &" "& oRecord.GetFieldData(i)
next
for each oField in oRecord
wscript.echo oField.ID &" "& oField.Data
next
The latter one looks best but internally costs two extra objects (the
enumerator and the field objects), while the former one does the same, but
is not as flexible -- a "for each" can easily internally skip records on the
run if needed (think filtering), a "for" loop needs the full count upfront,
so no sneaky dropping of certain entries.
That's how I see it as well. Your call, of course, depending on what
your preference is (or requirement is) for parsing the record data.<g>
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
R.Wieser
2016-01-28 08:49:55 UTC
Permalink
GS,
Post by GS
That's how I see it as well. Your call, of course, depending
on what your preference is (or requirement is) for parsing
the record data.<g>
To be honest ... I think I will probably end up implementing both, so I
can choose whatever is best for a certain situation. :-)

Regards,
Rudy Wieser
Post by GS
Post by R.Wieser
for i=0 to oRecord.FieldCount-1
wscript.echo oRecord.GetFieldID(i) &" "& oRecord.GetFieldData(i)
next
for each oField in oRecord
wscript.echo oField.ID &" "& oField.Data
next
The latter one looks best but internally costs two extra objects (the
enumerator and the field objects), while the former one does the same, but
is not as flexible -- a "for each" can easily internally skip records on the
run if needed (think filtering), a "for" loop needs the full count upfront,
so no sneaky dropping of certain entries.
That's how I see it as well. Your call, of course, depending on what
your preference is (or requirement is) for parsing the record data.<g>
--
Garry
Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Ulrich Möller
2016-01-27 00:30:55 UTC
Permalink
Post by R.Wieser
Hello all,
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)
Regards,
Rudy Wieser
I think there is no equivalent in vbscript, but perhaps this can be an
alternative for you:

' VB Script Document
dim avnt(1) ' One dimensional variant array with 2 elements
dim v

' Initialize the array
avnt(0) = Array("Key1", "Value1")
avnt(1) = Array("Key2", "Value2")

' Access the array in a foreach loop
For Each v In avnt
wscript.echo v(0), v(1) ' => outputs key(n) value(n) pairs
Next

' Direkt access the array by index values
wscript.echo avnt(0)(0) ' outputs "Key1"
wscript.echo avnt(0)(1) ' outputs "Value1"
wscript.echo avnt(1)(0) ' outputs "Key2"
wscript.echo avnt(1)(1) ' outputs "Value2"

Ulrich
R.Wieser
2016-01-27 10:40:09 UTC
Permalink
Ulrich,
Post by Ulrich Möller
I think there is no equivalent in vbscript,
Others have told me as much too, so I guess I have to believe it. :-)
Thank you, but I allready did all of that. I just went on to see if there
where other possibilities. On of them being the mentioned "for each" with
not one, but two (or more?) returned results.

Alas, its not available, so I have to let go of the ideas I had for if it
would be ... :-\

Regards,
Rudy Wieser
Post by Ulrich Möller
Post by R.Wieser
Hello all,
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
Post by Ulrich Möller
Post by R.Wieser
does not have it either. :-)
(Full disclosure: I'm implementing a IEnumVariant object)
Regards,
Rudy Wieser
I think there is no equivalent in vbscript, but perhaps this can be an
' VB Script Document
dim avnt(1) ' One dimensional variant array with 2 elements
dim v
' Initialize the array
avnt(0) = Array("Key1", "Value1")
avnt(1) = Array("Key2", "Value2")
' Access the array in a foreach loop
For Each v In avnt
wscript.echo v(0), v(1) ' => outputs key(n) value(n) pairs
Next
' Direkt access the array by index values
wscript.echo avnt(0)(0) ' outputs "Key1"
wscript.echo avnt(0)(1) ' outputs "Value1"
wscript.echo avnt(1)(0) ' outputs "Key2"
wscript.echo avnt(1)(1) ' outputs "Value2"
Ulrich
Ulrich Möller
2016-01-27 14:08:29 UTC
Permalink
Post by R.Wieser
Ulrich,
Post by Ulrich Möller
I think there is no equivalent in vbscript,
Others have told me as much too, so I guess I have to believe it. :-)
Thank you, but I allready did all of that. I just went on to see if there
where other possibilities. On of them being the mentioned "for each" with
not one, but two (or more?) returned results.
Alas, its not available, so I have to let go of the ideas I had for if it
would be ... :-\
Regards,
Rudy Wieser
Post by Ulrich Möller
Post by R.Wieser
Hello all,
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
Post by Ulrich Möller
Post by R.Wieser
use " foreach ($aList as $sKey => $sValue)". Is there something
equivalent
Post by Ulrich Möller
Post by R.Wieser
in/under VBScript ?
Remark: I'm not looking for the dictionary object. I know that one.
It
Post by Ulrich Möller
Post by R.Wieser
does not have it either. :-)
(Full disclosure: I'm implementing a IEnumVariant object)
Regards,
Rudy Wieser
I think there is no equivalent in vbscript, but perhaps this can be an
' VB Script Document
dim avnt(1) ' One dimensional variant array with 2 elements
dim v
' Initialize the array
avnt(0) = Array("Key1", "Value1")
avnt(1) = Array("Key2", "Value2")
' Access the array in a foreach loop
For Each v In avnt
wscript.echo v(0), v(1) ' => outputs key(n) value(n) pairs
Next
' Direkt access the array by index values
wscript.echo avnt(0)(0) ' outputs "Key1"
wscript.echo avnt(0)(1) ' outputs "Value1"
wscript.echo avnt(1)(0) ' outputs "Key2"
wscript.echo avnt(1)(1) ' outputs "Value2"
Ulrich
Hi Rudy,

perhaps you missed something. My suggestion uses a mixed Array (array of
array) in contrast to a 'normal' Array. So you will be able to simulate
such approach you want. Simply put as many arrays with key/value pairs
you want into another surrounding variant array.

see also https://en.wikibooks.org/wiki/Visual_Basic/Arrays on how
vb/vbscript deals with arrays.

Ulrich
R.Wieser
2016-01-27 14:51:59 UTC
Permalink
Ulrich,
Post by Ulrich Möller
perhaps you missed something. My suggestion uses a mixed
Array (array of array) in contrast to a 'normal' Array.
My apologies for not giving you a full response.

I think I've been implementing pretty much every possible way to return the
data back from the object to the script, including all kinds of array usage,
ranging from a full record in a two-dimensional array, upto returning a per
field, single dimension two element array (retrieved by supplying the field
index to the record object).

The only thing I did not do was to return an array object of array objects,
as I already returned a two-dimensional "normal" array. For the same reason
I've not tried to return a single-field array object, as I could already
return a single dimension "normal" array. Though this does not mean I've
not considered it. :-)

To be honest, although arrays-as-a-result are easy enough to implement and
return I found them to be quite ugly looking (in the scripting code they are
retrieved by and used in).

Regards,
Rudy Wieser
Post by Ulrich Möller
Post by R.Wieser
Ulrich,
Post by Ulrich Möller
I think there is no equivalent in vbscript,
Others have told me as much too, so I guess I have to believe it. :-)
Thank you, but I allready did all of that. I just went on to see if there
where other possibilities. On of them being the mentioned "for each" with
not one, but two (or more?) returned results.
Alas, its not available, so I have to let go of the ideas I had for if it
would be ... :-\
Regards,
Rudy Wieser
Post by Ulrich Möller
Post by R.Wieser
Hello all,
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
Post by Ulrich Möller
Post by R.Wieser
use " foreach ($aList as $sKey => $sValue)". Is there something
equivalent
Post by Ulrich Möller
Post by R.Wieser
in/under VBScript ?
Remark: I'm not looking for the dictionary object. I know that one.
It
Post by Ulrich Möller
Post by R.Wieser
does not have it either. :-)
(Full disclosure: I'm implementing a IEnumVariant object)
Regards,
Rudy Wieser
I think there is no equivalent in vbscript, but perhaps this can be an
' VB Script Document
dim avnt(1) ' One dimensional variant array with 2 elements
dim v
' Initialize the array
avnt(0) = Array("Key1", "Value1")
avnt(1) = Array("Key2", "Value2")
' Access the array in a foreach loop
For Each v In avnt
wscript.echo v(0), v(1) ' => outputs key(n) value(n) pairs
Next
' Direkt access the array by index values
wscript.echo avnt(0)(0) ' outputs "Key1"
wscript.echo avnt(0)(1) ' outputs "Value1"
wscript.echo avnt(1)(0) ' outputs "Key2"
wscript.echo avnt(1)(1) ' outputs "Value2"
Ulrich
Hi Rudy,
perhaps you missed something. My suggestion uses a mixed Array (array of
array) in contrast to a 'normal' Array. So you will be able to simulate
such approach you want. Simply put as many arrays with key/value pairs
you want into another surrounding variant array.
see also https://en.wikibooks.org/wiki/Visual_Basic/Arrays on how
vb/vbscript deals with arrays.
Ulrich
t***@cybernet.co.jp
2016-01-27 01:13:29 UTC
Permalink
You can use Scripting.Dictionary object:

Dim aList
Set aList = CreateObject("Scripting.Dictionary")
aList.Add "a", "123"
aList.Add "b", "456"
aList.Add "c", "789"

Dim sKey
For Each sKey In aList.Keys
Dim sValue
sValue = aList(sKey)
WScript.Echo "key : [" & sKey & "], value = [" & sValue & "]"
Next
Post by R.Wieser
Hello all,
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)
Regards,
Rudy Wieser
Mayayana
2016-01-27 02:28:01 UTC
Permalink
He already said he doesn't want to use that. And
he won't say what he wants to accomplish. Apparently
it's just a curiosity project without any practical aim
and he just wants to explore options.
R.Wieser
2016-01-27 11:41:14 UTC
Permalink
Mayayana,
And he won't say what he wants to accomplish.
You misunderstood. I could tell you exactly what I'm doing now. But that
would be rather counter productive, as I would get all kinds of answers
having absolutily *nothing* to do with the information I'm seeking.

Information I need for my current project-at-hand, *but could also use in
other, future ones*.

"Give a hungry man a fish, and he will be back tomorrow, again hungry.
Learn a man to fish, and he will never be hungry again"

You guys are constantly offering me fish, while I want to lean *how* to fish
...
Apparently it's just a curiosity project without any practical aim
Nope. It has a practical aim. One I'm pretty sure I can reach myself, as
long as I can get some info about small stuff I encounter along the way.

... actually, the result is already running, but I want to improve on it.
and he just wants to explore options.
Not "just". I want to know what my options are, so that *the next time* I
write something and encounter a similar problem I can choose which of those
options are the best to solve it.

So, please learn me to fish, and stop trying to push fishes into my hands.
:-)

Regards,
Rudy Wieser

P.s.
The same goes for most other posts in which I ask for information. Please
stop with second-guessing what I actually need. I would like to graciously
accept whatever help someone is offering, but what I'm asking for needs to
be answered first (not last, or more often, never).

P.p.s.
Mayayana, you seem to think that knowing what I'm busy with will help you to
better understand the question, and (assumingly) thereby be able to answer
the question.

I'm going to exactly tell you what I'm at, and in the end I expect you to
tell me if it helped you at all to do so. And no, offering other methods to
do something similar does not count, as I've already implemented a few
(which I already have mentioned in this thread), and have one or two others
on stock.

My current project (of sorts) is creating an ActiveX component which will be
able to retrieve data from an Outlook Express .DBX file. I wished to use a
possible two-result "for each" VBScript command to return a fields ID and
accompanying data from the stored records.

So, did it help ? ;-D
He already said he doesn't want to use that. And
he won't say what he wants to accomplish. Apparently
it's just a curiosity project without any practical aim
and he just wants to explore options.
Evertjan.
2016-01-27 11:44:10 UTC
Permalink
Post by R.Wieser
So, please learn me to fish, and stop trying to push fishes into my hands.
Programming you learn yourself.

However your to-the-point OQ was clearcut
and got you my primary straight answer "no".
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
R.Wieser
2016-01-27 12:21:09 UTC
Permalink
Evertjan,
Post by Evertjan.
Programming you learn yourself.
Very true. But you need to know the syntax before you can start to (try
to) throw several elements together to create a program. Right ?

And for the record, my question was all about syntax, not programming.

Regards,
Rudy wieser
Post by Evertjan.
Post by R.Wieser
So, please learn me to fish, and stop trying to push fishes into my hands.
Programming you learn yourself.
However your to-the-point OQ was clearcut
and got you my primary straight answer "no".
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
R.Wieser
2016-01-27 10:43:48 UTC
Permalink
ttanaka,
No, I cannot. As I mentioned in another message in this thread, I am not a
100% sure that the "key" is unique.

And yes, I also thought of using it. It would have made accessing the data
a lot easier. :-)

Thanks for your suggestion though.

Regards,
Rudy Wieser


-- Origional message:
<***@cybernet.co.jp> schreef in berichtnieuws
2b36d537-18b8-4610-9eaa-***@googlegroups.com...
You can use Scripting.Dictionary object:

Dim aList
Set aList = CreateObject("Scripting.Dictionary")
aList.Add "a", "123"
aList.Add "b", "456"
aList.Add "c", "789"

Dim sKey
For Each sKey In aList.Keys
Dim sValue
sValue = aList(sKey)
WScript.Echo "key : [" & sKey & "], value = [" & sValue & "]"
Next
Post by R.Wieser
Hello all,
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)
Regards,
Rudy Wieser
k***@gmail.com
2019-03-19 23:00:07 UTC
Permalink
Post by R.Wieser
Hello all,
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)
Regards,
Rudy Wieser
' 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
End Select
I = I + 1
Next
Erase ab

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.
Loading...