running a loop on a comma delimited list of items progress 4GL
Asked Answered
A

3

8
def var cList as char no-undo.
assign cList = "one,two,three,four".
<Loop> cList
logic...
</Loop>

What's the best way to loop through a comma delimited list in a char variable so that in this example I would get one then two then three then four.

Andie answered 3/4, 2012 at 13:35 Comment(0)
D
5

Lol I still remember a bit of Progress I think.

DEF VAR i AS INT NO-UNDO.
&SCOPED-DEFINE LIST "one,two,three,four"

DO i=1 TO NUM-ENTRIES({&LIST}):
  MESSAGE SUBSTITUTE("LIST[&1] is &2", i, ENTRY(i, {&LIST})).
END.
Demonography answered 3/4, 2012 at 13:59 Comment(0)
F
4
DEFINE VARIABLE ch-list     AS CHARACTER    NO-UNDO.
DEFINE VARIABLE i-cnt       AS INTEGER      NO-UNDO.
DEFINE VARIABLE i-entry     AS INTEGER      NO-UNDO.

ASSIGN
    ch-list = "one,two,three,four"
    .

ASSIGN
    i-cnt = NUM-ENTRIES(ch-list)
    .

REPEAT i-entry = 1 TO i-cnt:

    DISPLAY
        ENTRY(i-entry, ch-list)
        WITH DOWN.

END.
Fantoccini answered 3/4, 2012 at 13:56 Comment(3)
Is there a specific reason that the NUM-ENTRIES function isn't in-lined in the repeat statement? Something to do with it having to be evaluated each iteration through the loop?Serg
Yes - REPEAT ... TO evaluates the function on every iteration of a loop, so for better performance get the num-entries value outside of the loop, store it in a variable, and use the variable.Fantoccini
or do a negative loop (Repeat i-entry = NUM-ENTRIES(ch-list) to 1 by -1) as only 2nd element is re-evaluated on each iteration, but you working through list backwards.Goulden
I
0
 DEFINE VARIABLE iNumEntries AS INTEGER NO-UNDO.

 DEFINE VARIABLE iLoop AS INTEGER NO-UNDO.

 def var cList as char no-undo.

 assign cList = "one,two,three,four".

 ASSIGN iNumEntries = NUM-ENTRIES(cList,",").

 DO iLoop = 1 TO iNumEntries:

      MESSAGE ENTRY(iLoop,cList,",") VIEW-AS ALERT-BOX.

      /* You can use display, assign to variable, etc */

 END.
Integrated answered 27/8, 2015 at 9:48 Comment(2)
Your answer is just a block of code with no explanation, and for that reason, it is liable to be deleted.Destinee
Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Livesay

© 2022 - 2024 — McMap. All rights reserved.