Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Looping in Harlow

Hi
How do you loop in Harlow
E.g. equivilent of 'For' loop, 'while', etc

Comments

  • Currently Harlowe does not have an equivalent for/while/loop macro.

    There are a couple of ways to emulate the functionality depending on exactly what you are trying to do within the loop.

    One method is to use a (live:) macro with an embedded (stop:) macro.
    (set: $counter to 0)
    (live: 50ms)[
    	Do something.....
    	(set: $counter to it + 1)
    	(if: $counter is 10)[(stop:)]
    ]
    
  • Thanks, that works but is there any way to stop it refreshing the passage window with each iteration?
  • edited January 2016
    Not that I know of.

    From your comment I am guessing that you are using the 'looping' to update the screen on each 'iteration' (firing of the timer event). The (live:) macro was not designed for the purpose it's being used for in this use-case.

    note: The (live:) macro is a background thread based timer event and as far as I know the refreshing is due to the process focus switching between the foreground thread that allows the user to interact with the screen and the background thread used by the timer, which triggers a screen refresh due to the page being updated.

    Another method is to use passage recursion to simulate looping, the following example consists of two passages: Main and Display Element

    a. Main passage
    (set: $list to (array: "Element 1", "Element 2", "Element 3", "Element 4", "Element 5"))
    (set: $index to 0)
    
    Using a loop to display the contents of an array:
    (display: "Display Element")
    
    b. Display Element passage:
    (set: $index to it + 1)(print: $list's ($index))
    (if: $index < $list's length)[(display: "Display Element")]
    
    note: the problem with this methods is that the contents of each iteration is nested/embedded within the content of the previous iteration and there is a limit to the maximum depth that nesting can become before the web-browser throws an error.
Sign In or Register to comment.