Introduction to Scripts

Home, Up: Scripts

 

On this page:

The Basics, Creating a Script, Running a Script, The Echo Mode, The PROMPT Command, A First Example: Body Mass Index.

 

The Basics

A script is a collection of input lines that Hypatia reads from a file.

With a few differences, these are lines exactly as you would enter them directly.

There are two restrictions:

- Scripts cannot be nested -- that is, a script cannot call another script (nor can it call itself).

- Scripts cannot contain loop commands (DO or REPEAT).

A script can be called in a loop, though -- this is an important feature, and scripts will show their full potential in the context of loops.

There are a few commands that only make sense in a script, for instance the PROMPT command, IF ... THEN conditions, or the ENDLOOP command -- we'll come to them later.

 

The result file hy gets updated at the end of a script, within a script it only gets written to when a calculation line uses accumulation mode.

To uphold this principle, the command $ is not allowed in a script (you can use $ 0 + & instead).

 

Creating a Script

You have to create a script file with an editor. You can, but do not have to, use Hypatia's EDIT filename command.

Like all of Hypatia's files script files must be located in Hypatia's program folder.

The name of the file is the name of the script. The name must not contain spaces. No file extension is assumed, but you can use any you want.

You can have empty lines in your script, they will be ignored. For comments in a script, see the page after the next.

Because Hypatia only reads from a script file and does not write to it, you can keep it open in the editor while you work on it.

 

Running a Script

The command to execute, or run, a script is:

RUN filename

There is a short version of this command: an underscore character immediately followed by the file name (no space in between):

_filename

When you use RUN command the output to the screen is slightly more verbose than with the underscore version.

How to run a script in a loop will be discussed in the next chapter, "Loops".

 

The Echo Mode

The commands ECHO ON and ECHO OFF turn echo mode on or off (off is default).

ECHO on its own displays the current state of the echo mode, on or off.

With echo mode on, the lines in the script are displayed as they are processed, this is mostly meant to help with debugging.

 

The PROMPT Command

In most cases scripts will need to use variables.

Any variable that already exists before the script is called can be used by the script, but it is an important feature that you can enter values of variables while the script gets executed.

This is done with the PROMPT command, which prompts the user to enter a value which will be assigned to a variable:

PROMPT $variable comment

The user can enter a number, or a calculation -- the result will only be assigned to the variable, it will not update $ and will not be written to hy.

The variable can be one that already exists, or it can be a new one, in which case it will be created when a value is entered.

If an empty line is entered, an existing variable retains its value. A variable that does not yet exist will not be created, though.

Optionally a comment can be added after the variable name, to explain what value should be entered.

The comment will be displayed in lower case characters, but the first character will be in upper case.

 

A First Example: Body Mass Index

Let us write a simple script to see how it works.

Body mass index is calculated from height and weight: weight in kg divided by the square of height in m.

With the variables $kg and $cm for weight and height we have to divide the height by 100 to convert it to m, the formula then is $kg $cm 100 / SQ /

Since we hardly need the body mass index with 13 digits after the decimal point, let's round the result to just one: $kg $cm 100 / SQ / 1 ROUND

Now we can create our script, let's call it bmi:

? EDIT bmi

In the editor window that opens enter the following lines:

 

PROMPT $cm enter height in cm:

PROMPT $kg enter weight in kg:

$kg $cm 100 / SQ / 1 ROUND

 

Save the file, close the editor, and from now on you can use this script -- for example:

? _bmi

Enter height in cm:

? $cm = 172

Enter weight in kg:

? $kg = 64

= 21.6

 

In case you are in the US and want to use pounds, feet and inches, we'll deal with that on the next page.

 

Home, Up: Scripts, Next: IF ... THEN Conditions