ForEach by Delimiter

Started by JTaylor, October 03, 2025, 07:45:25 AM

Previous topic - Next topic

JTaylor

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

kdmoyers

hmmm, you got a smidge of code you could post? I'm curious. -K
The mind is everything; What you think, you become.

td

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
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

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

JTaylor

Also, I tried ObjectTypeGet() but it comes up blank on everything I try.

Jim

JTaylor

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

spl

Wonder how a switch through a count() would work.
Stan - formerly stanl [ex-Pundit]

JTaylor

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

JTaylor

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.

spl

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.
Stan - formerly stanl [ex-Pundit]

JTaylor

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

spl

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.
Stan - formerly stanl [ex-Pundit]

td

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?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

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

spl

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.
Stan - formerly stanl [ex-Pundit]

JTaylor

Yes.  It was a recent and WONDERFUL addition.


Jim

JTaylor

cursorTypes = "0,1,2,3"  

cnt = 1

ForEach cType in cursorTypes By ","

  Message(cnt, cType)

  cnt += 1

Next

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

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.
Stan - formerly stanl [ex-Pundit]

JTaylor

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


spl

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.
Stan - formerly stanl [ex-Pundit]

SMF spam blocked by CleanTalk