WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: JTaylor on October 03, 2025, 07:45:25 AM

Title: ForEach by Delimiter
Post by: JTaylor on October 03, 2025, 07:45:25 AM
Should   

    1,2,3,4

work with "ForEach by Delimiter" or must the values be strings?   The ForEach never ends if I use numbers.

Jim
Title: Re: ForEach by Delimiter
Post by: kdmoyers on October 03, 2025, 08:53:52 AM
hmmm, you got a smidge of code you could post? I'm curious. -K
Title: Re: ForEach by Delimiter
Post by: td on October 03, 2025, 08:58:18 AM
Foreach by delimiter only works with delimited lists. Delimited lists are always strings. You need to provide an example of exactly what you are referring to.

This works as expected.
list = '1,2,3,4'
test = ''
foreach n in list by ','
   test := n:' '
next
message('Test', test)
exit
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 03, 2025, 09:34:05 AM
Once it passes the 4th item the value of cType becomes blank.



;This never ends
cursorTypes = cs.adOpenForwardOnly:",": cs.adOpenKeyset:",": cs.adOpenDynamic:",": cs.adOpenStatic   

 ;This works as expected.
;cursorTypes = 0:",":1:",":2:",":3   

Message(ItemCount(cursorTypes,","),cursorTypes)  ;They both look exactly the same.

cnt = 1
ForEach cType in cursorTypes By ","
  Message(cnt, cType)
  cnt += 1
Next
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 03, 2025, 09:36:44 AM
Also, I tried ObjectTypeGet() but it comes up blank on everything I try.

Jim
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 03, 2025, 09:56:58 AM
If it helps.  The items in the collection are VT_I4 and then bundled up and passed back like this:

CRecordSetConstants* constants = new CRecordSetConstants();
pVarResult->vt = VT_DISPATCH;
pVarResult->pdispVal = constants;

Jim
Title: Re: ForEach by Delimiter
Post by: spl on October 04, 2025, 05:58:17 AM
Wonder how a switch through a count() would work.
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 04, 2025, 09:00:48 AM
Just tried this and it solves the presenting problem so it appears it is something with the object type since I assume the addition action converts it.  I have had similar problems before with ADODB where I had to add zeroes to get it to see something as a number.  Hadn't run into the ForEach issue though.


cursorTypes = cs.adOpenForwardOnly+0:",": cs.adOpenKeyset+0:",": cs.adOpenDynamic+0:",": cs.adOpenStatic+0
lockTypes   = cs.adLockReadOnly+0:",": cs.adLockOptimistic+0:",": cs.adLockPessimistic+0:",": cs.adLockBatchOptimistic+0


Jim
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 04, 2025, 09:01:23 AM
Trying to think how one would handle the variable number of items.

Jim

Quote from: spl on October 04, 2025, 05:58:17 AMWonder how a switch through a count() would work.
Title: Re: ForEach by Delimiter
Post by: spl on October 04, 2025, 10:21:42 AM
Quote from: JTaylor on October 04, 2025, 09:01:23 AMTrying to think how one would handle the variable number of items.

Yes, but even a variable amount of items could have a count, unless the items were from a stream, in which a different type of looping process would be called for. You originally asked about a delimited list that was not a string [as Tony pointed out]. Having perhaps ruling out foreach w/delimiter... seems you could present items as an array, or another .Net construct. Having introduced how your items would be introduced to WB, does raise a question, so my comments are not confrontational, just having clarity with my bankers rounding post.
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 04, 2025, 12:30:43 PM
Yeah, it was sort of a stupid question about it not being a string.  What I meant was could it just be 1,2,3,4, that is numbers separated by commas, which is a string.  Hopefully what I actually meant came through.  Does seem to be something with the variants make it act wonky.

This is really an odd thing I did and normally this situation would never arise as I formulated that as part of my testing on a new project and was a bit unusual.  Probably good if it was sorted out in the ForEach code so someone else doesn't spend an hour or two trying to figure out what they did wrong only to find out it was not their fault :-)
 

Jim
Title: Re: ForEach by Delimiter
Post by: spl on October 04, 2025, 12:44:50 PM
Equally stupid of me, thinking the PS.....

$x = "1,2,3,4" #comes out as [String] type
$x = 1,2,3,4 #comes out as Array

by default.
Title: Re: ForEach by Delimiter
Post by: td on October 07, 2025, 09:46:48 AM
This problem is only tangentially related to "foreach" looping structures. It was around before foreach was added to WIL. It will be addressed in the next release.

It does beg the question. Why not just implement an IEnumerator interface on your COM object, assuming its primary or even secondary usage is as a collection?
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 07, 2025, 10:14:32 AM
As I mentioned, this was a bit of a weird use of that collection and not something I would ever envision anyone doing in real life.  Also, that is not a collection I would ever see anyone enumerating.  Most collections I do enable enumeration.   This one is simply there to provide access to named constants.

In any event, seems like it pointed out a potential weakness.  Thanks.

Jim
Title: Re: ForEach by Delimiter
Post by: spl on October 07, 2025, 12:24:45 PM
So for us who need a catch-me-up. The basic question for WIL: is a string value enumerable [which probably is], and therefore can be treated in a foreach loop as a collection object? Or not.
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 07, 2025, 12:45:16 PM
Yes.  It was a recent and WONDERFUL addition.


Jim
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 07, 2025, 12:47:26 PM
cursorTypes = "0,1,2,3" 

cnt = 1

ForEach cType in cursorTypes By ","

  Message(cnt, cType)

  cnt += 1

Next
Title: Re: ForEach by Delimiter
Post by: td on October 07, 2025, 02:27:30 PM
Quote from: JTaylor on October 07, 2025, 10:14:32 AMAs I mentioned, this was a bit of a weird use of that collection and not something I would ever envision anyone doing in real life.  Also, that is not a collection I would ever see anyone enumerating.  Most collections I do enable enumeration.   This one is simply there to provide access to named constants.

In any event, seems like it pointed out a potential weakness.  Thanks.

Jim

The problem has been around for close to 20 years, so I am not sure if it was all that much of an issue for most users. However, it is good to correct any and all defects when they reveal themselves. Thanks for mentioning it.

And I did include a conditional phrase at the end of the sentence about the IEnumerate interface.
Title: Re: ForEach by Delimiter
Post by: spl on October 08, 2025, 05:58:26 AM
Quote from: JTaylor on October 07, 2025, 12:45:16 PMYes.  It was a recent and WONDERFUL addition.
Jim

Yes, but of course, your example is really only another way to process itemlist() or any other delimited string. Your original example was for cursortype properties for an ADO recordset. I always thought they were an enumeration. Seems you thought that if the string variables were typed the foreach would return the numeric equivalent.

I could be totally off-base about that, but it doesn't really matter much to what I understand how WB handles data...

hash tables [i.e. WB maps]
Arrays
recordsets [i.e. .Net datatypes]

would be my options with foreach, otherwise another construct would obtain the desired results.
Title: Re: ForEach by Delimiter
Post by: JTaylor on October 08, 2025, 07:14:08 AM
Not that collection.   As I mentioned, couldn't foresee a reason anyone would find that useful as all you would get would be a bunch of numbers, many of of them the same value since GetConstants() returns most all of the ADODB constant Enums.

It returned the right values except something about the typing threw off Foreach so that it never stopped looping.

I really like this option because sometimes you have a delimited string you want to loop through and previously you had to either convert it to something else or use a For loop with ItemExtract and ItemCount.

Jim

Title: Re: ForEach by Delimiter
Post by: spl on October 08, 2025, 10:09:27 AM
Understood. But the real issue is not to cogitate over how to represent, extract, manipulate data but how individual apps can handle the above. Even PS differentiates between foreach and foreach-object.