Tuesday, October 23, 2007

[Protium] Splitting

Here's a short script for splitting a file. The file in this case is the perl language data file from the PLEAC website. pleac_perl.data contains sections marked with "# ^^PLEAC^^_" followed by a number. The section applies until the next section is marked. The script splits each section out of pleac_perl.data into pleac_perl_number.p. I'm working through these files and re-expressing the intent of the perl code in Protium.

The code may be described as follows:
  1. Define a work area, or alias, for the data file, calling it 'pleac'.
  2. Open the text file within that work area, using define-file-lit.
  3. Start a loop which iterates through an enumeration of the lines of data in the work area.
  4. Let a variable contain the data in the current line of the work area.
  5. Test whether the regular expression works for the current line.
  6. If it does work, let a variable contain the number found, ...
  7. ... turn off any output redirection, ...
  8. ... and redirect output to the file 'pleac_perl_' + the number found (in the same directory as the script).
  9. End of the if.
  10. Output the contents of the line.
  11. End of the iteration.
Simple ... and way powerful!

© Copyright Bruce M. Axtens, 2007