Loop Example: Centimeters-Feet-Inches Table

Home, Up: Loop and Script Examples

 

In this example:

BUFFER, ENDLOOP, Accumlation Mode, Writing a 2-dimensional Table.

 

This example demonstrates how Hypatia can write two-dimensional tables to a file.

In chapter "Scripts" we have created a script to convert Centimeters to Feet and Inches -- using it, we will now create a centimeters, feet and inches table for centimeter values from 50 to 200.

We will not use the loop index I, but use a variable $cm that we give a start value of 50 and that we increase by 1 at each pass of the loop -- it would be trivial to use a different increment.

For each $cm value we calculate the variables $feet and $inch (for how this is done, see the example in "Scripts"), then $cm gets written to the buffer starting a new line, followed by $feet and $inch on the same line (using accumulation mode suffixes && and &).

To write the value of a variable to the buffer we need a calculation line in accumulation mode that consist only of this variable, making its value the calculation result.

When $cm has exceeded 200 the ENDLOOP command is given, and the I*: lines write the buffer to a file, end buffer mode and open the file with our result table.

Let's call our script cm-table -- we can create it with EDIT cm-table -- and the file to which we write the table cm-feet-inches.txt (from Hypatia's point of view, the .txt file extension is arbitrary).

 

I1: BUFFER START

I1: $cm = 50

$feet = $cm 100 / :FT

$inch = $feet FRAC 12 * 0 ROUND

$feet = $feet INT

IF $inch 12 EQUAL THEN $feet = $feet 1 +

ALSO: $inch = 0

$cm &&

$feet &

$inch &

$cm = $cm 1 +

IF $cm 200 >> THEN ENDLOOP

I*: BUFFER SAVE cm-feet-inches.txt

I*: BUFFER DISCARD

I*: EDIT cm-feet-inches.txt

 

Now we have to run this script in an infinite loop:

? _*cm-table

and it will create our "cm, feet and inches" table, and open it in the external editor (by default, notepad.exe):

 

50 1 8

51 1 8

52 1 8

53 1 9

54 1 9

...

196 6 5

197 6 6

198 6 6

199 6 6

200 6 7

 

Hypatia will also show the line

= 7

which, as always after a loop, is the last calculation result, whether we need it or not.

 

Home, Up: Loop and Script Examples, Prev: Loop Example: Rectangles, Next: