Programmer's WorkBench (pwb.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
Macro Looping Examples
 Description                               Up Contents Index Back
─────Programmer's WorkBench─────────────────────────────────────────────────
 
     Macro conditionals allow you to write loops, as shown in the
     following examples.
 
     This macro executes the macro 'Act' for every line in the file,
     starting from the end of the file. You can define Act to be any
     action you want.
 
          AllBackward := Endfile :>s Act Up +>s
 
     The following macro scans forward a line at a time. However, it has
     the limitation that it executes Act for a window of lines past the
     end of the file. Depending on the action, this may be benign.
 
          AllForward := Begfile :>s Act Down +>s
 
     A macro that scans forward and stops on the last line of the file
     is slower because the termination test is costly. This macro also
     clears the anchor as a side effect.
 
          AllForward2 := Begfile Savecur :>s Restcur act   \
                         Down Savecur Begline Endfile +>s
 
     In practice, you rarely need to perform an action on every line.
     More common (and faster) is the "file loop" where an action is
     repeated for each occurrence of a text pattern (regular expression)
     in the file. These macros generally take the following form:
 
          For1 := Begfile :>s Arg Arg "RE" Psearch -> Act =>s
 
     Where 'RE' is a regular expression that matches a location of
     interest, and 'Act' is a macro or function sequence that performs
     the desired action. If the action does not perform a search, the
     macro runs faster if the pattern is specified only once before the
     beginning of the loop, as follows:
 
          For2 := Begfile Arg Arg "RE" :>s Psearch -> act =>s
 
     This takes advantage of the behavior of Psearch with no arguments,
     which repeats the previous search. When a regular expression is
     used, it is compiled only once at the beginning of the loop.
     See: Regular Expressions in Macro Strings
                                    -♦-