Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / DO TO loop variable not updated

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. DO TO loop variable not updated
#10768
Posted by: Michael S 2007-06-27 00:14:45 Last edited by: Jeff Glatt 2007-06-27 14:36:46 (Total edited 2 times)
I could swear that this worked before the latest updates
test.0 = 7
test.1 = 'This is line 1'
test.2 = 'This is line 2'
test.3 = 'This is line 3'
test.4 = 'This is line 4'
test.5 = ''
test.6 = 'This is line 6'
test.7 = 'This is line 7'

DO i = 1 TO test.0
  SAY 'test.0 =' test.0 'i =' i
  IF test.i == '' THEN DO
    STEMDELETE('test.', ,i , 1)
    /* Now we ALSO need to adjust i back one */
    i = i - 1
  END
END
What's happening is that, even though I've deleted an entry in the stem variable (and therefore test.0 gets decremented), the DO TO loop contains the original value of test.0 rather than the updated (ie, decremented) value.
Message2.
#10780
Posted by: Jeff Glatt 2007-06-27 14:33:01
Ok, this should never have worked. In a REXX DO TO, the "end expression" in the DO statement is evaluated once when the loop starts. It's not reevaluated every time through the loop. So the value of test.0 is evaluated once (to be 7) and that's it for the rest of the loop. You can change the value of test.0 itself inside the loop, but that doesn't effect the number of iterations of the loop.

If you're going to be changing the value of some variable used in the test expression, don't use a DO TO loop. Use something like DO WHILE or DO UNTIL, which evaluates the expression each time through the loop.
test.0 = 7
test.1 = 'This is line 1'
test.2 = 'This is line 2'
test.3 = 'This is line 3'
test.4 = 'This is line 4'
test.5 = ''
test.6 = 'This is line 6'
test.7 = 'This is line 7'

i = 1
DO WHILE i <= test.0
  SAY 'test.0 =' test.0 'i =' i
  IF test.i == '' THEN
    STEMDELETE('test.', , i, 1)
  ELSE
    i - i + 1
END
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Fri 2024-3-29  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0