Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing the increment inside a loop

    I would like to, when certain criteria are met, change where I'm at in a loop. Here is a MWE. In this code, I would like to cycle normally through v = 1, then 2, 3, 4, 5, but then skip to 56, 57, 58, 59. (I'm envisioning that the value of v is 5, then I add 50 to it, thus 55. Then when it goes back to the top of the loop it would increment to 56. Hence no 55 in the desired output below.) I thought that simply referring to the same local macro name would do it, but obviously I'm wrong.

    Code:
    forvalues v = 1/59 {
        display "I just want to check on this value:`v'"
        if `v' == 5 local v = `v' + 50
    }
    Desired output:

    I just want to check on this value:1
    I just want to check on this value:2
    I just want to check on this value:3
    I just want to check on this value:4
    I just want to check on this value:5
    I just want to check on this value:56
    I just want to check on this value:57
    I just want to check on this value:58
    I just want to check on this value:59


  • #2
    maybe,
    Code:
    forval i = 1/59 {
        if inrange(`i',1,5) | inrange(`i',56,59) {
        display "I just want to check on this value: `i'"
        }
    }

    Comment


    • #3
      As you have discovered, you cannot control a -forvalues- iterator in the way you wish. You do actually succeed in changing the value of `v' with your -local v = `v' + 50- command. (If you put a -display `v'- command after that you will see that `v' was increased to 55.) And it will stay that way for the rest of that iteration, but when you then go back to the top of the loop, the -foreach- is back in control.

      So you have to do this in a more old-fashioned way where you do the initialization, incrementing, and termination explicitly:
      Code:
      local v = 1
      while `v' <= 59 {
          display "I just want to check on this value: `v'"
          if `v' == 5 {
              local v = `v' + 50
          }
          local ++v
      }
      Added: Crossed with #2 & #4 (!)
      Last edited by Clyde Schechter; 28 Aug 2022, 12:46.

      Comment


      • #4
        Øyvind Snilsberg, That does give the desired output, and thank you for that. Unfortunately, that won't work in my (more complicated) situation as it's fiddling with when to display the output rather than fiddling with where in the loop I'm currently executing.
        Last edited by Michael Costello; 28 Aug 2022, 12:44.

        Comment


        • #5
          I think that #4 was written before #3 was posted. Anyway, Michael Costello , I'm guessing your response in #4 does not apply to the suggestion in #3. Take a look at it if you haven't already seen it. If you have, please explain more clearly why it would not be suitable for your full purposes.

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            As you have discovered, you cannot control a -forvalues- iterator in the way you wish. You do actually succeed in changing the value of `v' with your -local v = `v' + 50- command. (If you put a -display `v'- command after that you will see that `v' was increased to 55.) And it will stay that way for the rest of that iteration, but when you then go back to the top of the loop, the -foreach- is back in control.

            So you have to do this in a more old-fashioned way where you do the initialization, incrementing, and termination explicitly:
            Code:
            local v = 1
            while `v' <= 59 {
            display "I just want to check on this value: `v'"
            if `v' == 5 {
            local v = `v' + 50
            }
            local ++v
            }
            Added: Crossed with #2 & #4 (!)
            Yes, that worked splendidly Clyde, thank you kindly!

            Comment

            Working...
            X