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.

Bill
  • 1,237
  • 4
  • 21
  • 44

3 Answers3

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.
Abe Voelker
  • 30,124
  • 14
  • 81
  • 98
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.
Tim Kuehn
  • 3,201
  • 1
  • 17
  • 23
  • 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? – John Cleaver Sep 23 '15 at 15:07
  • 1
    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. – Tim Kuehn Sep 23 '15 at 19:41
  • 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. – AquaAlex Feb 11 '16 at 14:43
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.
  • Your answer is just a block of code with no explanation, and for that reason, it is liable to be deleted. – Wai Ha Lee Aug 28 '15 at 08:17
  • 2
    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. – NathanOliver Aug 28 '15 at 14:51