Home    Cart    Free Download    Manual

Installation:
  Free Downloads
  The H2O Family
  Install Notes

Programming:
  Prerequisites
  H2O Whitepaper
  Online Manual
  Code Examples
  H2O Free Support


What's H2O?
H2O is programming made for the web.

What's it like?
H2O is English-like. If you know some Perl, VB, ASP, or PhP you'll be immediately productive in H2O. It runs on Linux, Mac, Unix, and Windows.

How do I try it?
Download H2O for free. Get it from hosting providers. Or buy on-line.

Where does H2O come from?
The language was invented by Aestiva. H2O stands for:
   HTML with
   HTML/OS
   Overlays.


Chapter 5:
Variables, Conditionals, and Loops
The H2O development environment uses variables to carry data between HTML forms, tags, calculations, fields in databases and files. The same variable environment is used throughout. In this chapter we review the rules, requirements, limitations and conventions used when working with H2O variables.

We also discuss how to use IF-THEN statements, FOR loops, how to read in information from comma-delimited text files, how to work with two-dimensional data, and how to work with variables in HTML forms.

Starting with the how to write a variable name:

1. Variable names are case-insensitive
2. Variable names cannot begin with a number
3. Variable names cannot contain special characters other than dot or an underscore.
4. Variable names must not be the same as the name of a tag or function.

Examples of valid variable names include:

1. data14
2. cart_price
3. myfolder
4. acct.name.first

The last variable example above shows a variable containing dots. Note that dots in variable names are treated like other characters. Object-oriented languages such as Javascript, Java and C++ use the dot as a special character. H2O is a "functional" language. The dot has no special meaning. Examples of invalid variables include:

1. 14data
2. cart-price
3. my folder
4. acct$name$first

The first example above is invalid because it begins with a number. The second is invalid
because it includes a dash (which would conflict with a minus sign). The third contains a space which is not allowed. The last contains dollar signs which are special characters and not allowed.

When naming variables it is best to use words with meaning. It makes your programming more readable. Also, it's a good idea to prefix your names with the name of your application and an underscore. This ensures your variables don't conflict with Overlay tags (since Otags don't contain underscore characters).

As an example, when building a shopping cart application, use variable names like shop_cart, shop_taxrate, shop_shippingrate and so on.

The Default Value
The default value of all variables in H2O is ERROR. We'll talk more about this later.

Two-dimensionality
When using H2O it is important to remember that variables are two-dimensional. In other words, all variables contain one or more cells organized in columns and rows. The simplest of all variables, the "empty string," has one row and one column and nothing in it. A variable can never have less than one row or less than one column. The two-dimensionality of variables does not change how you perform simple calculations but gives you powerful capabilities that come in handy when working with delimited text files and when performing operations on large sets of information.

Specifying Variables
When working with variables you may specify the entire variable or only a specific cell. To specify the entire variable, write the name of the variable. To specify a specific cell, follow the variable name with the column and row number in square brackets as shown below. If you wish, you may specify only the column number. When the row is not specified the row number equals one.

For example cart_products refers to all the cells in the variable cart_products whereas cart_products[1,3] refers to the first cell in the third row of cart_products and
cart_products[3] refers to the cell in the third column of the first row.

The Math Convention
When using H2O, the cells in variables are specified by column and row, not by row and then column. Also, the first cell in a variable is at position 1,1, not at position 0,0. This is called the "Math Convention." It's quite intuitive unless you were trained as a computer programmer.

Most computer languages define the upper left position of an array as 0,0. And most often the row specified ahead of column. If you are familiar with programming languages you will want to remember that H2O uses the conventions used in mathematics, not computer science.

Variable Assignment
The correct way to assign values to variables is shown below:

1. my_variable = "-10"
2. my_variable = ROW("-10","10","20","30")
3. myvariable [2] = "10"
4. my_variable[3,4] = "-10"

Example 1 creates a new variable with one column and one row containing a negative ten. Data previously stored in my_variable is lost.

Example 2 creates a four column by one row variable containing the cells specified. Like the first example, data previously stored in my_variable is lost. The example uses the pre-defined tag called ROW to build the one-row variable.

In example 3, the value 10 is placed in column two, row one of my_variable. The row is one because it was not specified. Values in other cells are left alone.

Example 4 places a negative ten in the cell at column three, row four. The size of
my_variable is left unchanged unless it was expanded by virtue of placing a value in column 3, row 4. Other cells are left unchanged. It is important to note that although the default value of variables is a one column, one row cell containing the word ERROR, when expanding the size of a variable, all undefined cells are left empty. For example, if you were to write the two instructions:

mydata = "X"
mydata[3,3] = "X"

then mydata would look like:


    X  
    X  
    X X


Data Types
Each cell of a variable can store numbers, fractions, dates, times, logical values (TRUE or FALSE), small pieces of text or large pieces of text (like a text file).

There is no need to declare how many rows or columns you need in a variable. And there is no need to declare the type of data you put in each cell or how much data you expect to put in a cell. Just start using them. This feature is known as "auto-datatyping."

For example, suppose you want to create a variable called stooges containing three columns and one row with the cells containing the names Larry, Moe and Curly. You would write:

stooges=ROW("Larry","Moe","Curly")

Or suppose you have a text file called "mydoc.txt" containing ten thousand characters that you wish to load into the variable mytext. You would write:
COPY FILE="mydoc.txt" TO mytext /COPY


As you see, to create or use a variable you simply use it. There is no a need to declare it.

Using Different kinds of Variables
Although variables are "auto-datatyped" you still should know their limits and how they should be written. A summary is shown below:

  1. Logicals
    In H2O a "logical" is simply a word equal to TRUE or FALSE (case-insensitive). Some instructions, such as IF-THEN statements use these words to perform conditional operations. In addition, there are tags that produce logicals. For example, the tag ISFILE returns a TRUE or a FALSE depending on whether it finds a specified file. Consider the example below:

    << myimage="someimage.gif"
    IF ISFILE(myimage)="TRUE" THEN
    DISPLAY
    "<img src=" + myimage + ">"
    /DISPLAY
    ELSE
    DISPLAY
    "<img src=default.gif>"
    /DISPLAY
    /IF
    >>

    The IF-THEN statement runs what's within the THEN and the ELSE if the test between the IF and the THEN is true. If not, it runs the instructions between the ELSE and the /IF. The Overlay could also be written as:

    << myimage="someimage.gif"
    IF ISFILE(myimage) THEN
    DISPLAY
    "<img src=" + myimage + ">"
    /DISPLAY
    ELSE
    DISPLAY
    "<img src=default.gif>"
    /DISPLAY
    /IF
    >>

    The IF-THEN Statement
    Like all IF-THEN statements, H2O IF-THEN statements run instructions based on a "test" appearing between the IF and the THEN. The tests can be expressions which calculate to TRUE or FALSE or they can be tests that compare calculations to other calculations. The following comparisons operators can be used:

    = Equal?
    != Not equal? (Same as <> operator)
    > Greater than?
    >= Greater than or equal?
    < Less than?
    <= Less than or equals?
    ~ Begins with?
    ~~ Contains?

    Multiple "tests" can be combined together in a "Boolean Expression" to build more complex tests, as well. For example, consider the IF-THEN test below.

    IF (balance > 0 AND user_level >= 3) OR super_user="YES" THEN ..
    instructions go here
    /IF

    It performs instructions only if super_user is set to YES or if the user's balance is positive and their user_level is three or above. When combining tests use the words AND, OR or NOT, placing parentheses around each test.

  2. Dates and Times
    Dates are written as: MM/DD/YYYY HH:MM. The year YYYY can be replaced with a two-digit
    year whence the millenium 1900 will be assumed if YY is less than 20 and 2000 will be assumed if greater. If the time HH:MM is dropped then midnight is assumed. If the day MM/DD/YYYY is dropped then 01/01/2000 is assumed. This date format is the only date format used in H2O. Use it when working with cookies, databases and when performing date and time calculations. Date calculations work on dates going back to 500. Tags are available to display dates and times in multiple formats but they should be stored in the format used by H2O. For example, consider the Overlay below:

    << expire_date = ADDDAYS(TODAY,5)
    DISPLAY
    "Your demo will expire " + GETDATE(expire_date,"long")
    /DISPLAY
    >>

    The expiration date is stored properly but formatting is applied when you display the variable. The GETDATE tag, in this case, formats the number in a "long" format. A date like 05/01/2002 would display as May 1, 2002.
  3. Text
    Text, also known as "strings," may be zero characters up to 1 Gigabyte or
    the memory limit allowed for the process on the server (which may be less).

    Text can contain foreign characters, Tabs, Linefeeds, Carriage-Returns, HTML codes, programming tags, email messages, XML code, and complete or partial text documents.

  4. Integers
    Integers are whole numbers, positive or negative, up to eleven characters in length. Do not place special characters (such as $) or commas in integers. If you wish to reformat the number do so when you display it. For example:


    <<
    price = "50000"
    price = price * (1.0825)
    price_nice = FORMAT(price,"comma",2)
    DISPLAY price_nice /DISPLAY
    >>

    The price is stored properly but formatting is applied when you actually display the variable.

    The FORMAT tag, in this case, formats the number with commas, if needed, and two decimal places.
  5. Fractions
    Fractions are numbers, positive or negative, up to eleven characters in length. Like Integers, do not store commas or special characters in your fraction.

  6. Other Limits
    When working with variables keep in mind the following facts about variables:
      1. H2O variables may have up to XX columns and up to 100,000 rows.

      2. Unless otherwise specified, text searches, tags and comparisons are case-insensitive.
      The one exception is when specifying files. Unix and Linux and MacOS X systems
      are case-sensitive.


Global Variables
Variables in H2O carry their values from web page to web page. For example, if you write the instruction myname = "Yoshi" on one web page then five web pages later (unless you
edit the variable,) Myname will still be equal to "Yoshi." On the Web this is called state-persistence. In off-the-web environments this is called "A Global Variable Environment."

Whatever you call it, variables in H2O automatically save from page to page.



The exception to this rule is the case of "Local Variables." These are variables that can be defined inside functions. Local variables persist while inside the function but disappear outside of it.

Clearing Variables
To clear a variable, set it to ERROR. Clearing variables is useful if you are concerned about the total size of your variable environment. Since variables pass from page to page, they are saved on the Web server as users leave and return to the site. The larger the variable environment you use and the more users you have visiting your site, the more hard disk space is needed. It is a good idea to clear variables larger than 50,000 bytes if you do not need them.

If you clear a variable before the end of a page it will not be stored on the Web server.

State-Persistence Across The Network
Variables in H2O save from page to page, even as page control moves across a network. For example, suppose you write the instruction mynme="Yoshi" and the user moves from one copy of H2O to another (This is known as Server-Jumping). Then Myname will still be equal to "Yoshi."

Using Variables in Calculations
Performing calculations is a matter of using math operators and special tags to perform mathematical or statistical calculations. Operators include the four main math operators, plus (+), minus (-), multiple (*) and divide (/) and dozens of pre-defined Otags for performing math, logical and geometric calculations. Here are some examples of calculations:

1) i=i+1
2) taxrate = "0.875"
3) shipping_cost = SUMCOL(cart_products,"4")
4) APPEND product_line TO cart_products /APPEND

In the first example we take the variable i, increment it, and save it back into itself. This can also be written as i += 1.

In the next example we use the tag SUMCOL to sum over column four of the variable cart_products. We take the sum of column four and create a one-by-one variable called shipping_cost filled with the sum.

In the example after this we place the literal value "0.975" into the variable taxrate.

In the last example we append a row to a variable called cart_product. H2O also includes mathematical tags such as SIN, COS and TAN, an statistical functions to help you perform a wide variety of mathematical.

Variables, Literals and Parameters
Consider the instruction:

shipping_cost = SUMCOL(cart_products,"4")

The tag SUMCOL has two parameters. The first is a variable. The second is a value, known as a literal. It is in quotes. When writing tags it is important to know how to differentiate between literal values, variables and how to place them in tags. The following rules apply:

  1. Literal values should always be surrounded with quote characters except when the value is a positive integer, in which case quotes are optional.
  2. Three quote characters are supported; the single quote, the double quote and the caret (~). A literal value containing one kind of quote must be quoted with another kind of quote to avoid confusion.
  3. Variables cannot be surrounded with quotes, else they'll be understood as literals.
  4. Parameters of tags can be variables, literals or complete calculations. each must be separated from the next with a comma. The ability to set parameters to calculations is called nesting.

    Tags are nestable. In other words, a tag can be a parameter of a tag which
    is a parameter of another tag and so on. For example, the tag REPLACEALL is for performing a search and replace on a variable. It has three parameters; the first is the name of the variable being searched, the second is what you are searching for and the last is what you replace it with. To eliminate "<" characters from a variable and replace them with "<", the HTML code for a less than sign, you could write:

    myvar=REPLACEALL(myvar,"<","<")

    Now suppose you wanted to replace not only the less than sign, but also the greater than sign. You could write:

    myvar=REPLACEALL(myvar,"<","<")
    myvar=REPLACEALL(myvar,">",">")

    Or, taking advantage of "nesting" you could write:

    myvar=REPLACEALL(REPLACEALL myvar,"<","<"),">",">")


Using Variables in HTML
H2O variables are also called "names." The nomenclature is borrowed from the use of the word NAME in HTML. For example, the HTML input tag can contain NAME-VALUE pairs, as in:

<input type=text NAME=country VALUE="USA" size=15>

Since H2O is an "integrated environment" the NAMES in HTML forms and the variables in H2O are related. H2O variables fill "VALUES" in HTML forms.

NAMES in HTML forms, when submitted to the server, fill H2O variables. It's automatic. Suppose for example an HTML form like that below is stored on the Web server.

<html>
<< IF firstname="ERROR" THEN
firstname="John"
Lastname="Smith"
Gender="M"
/IF
>>
<form action=<<page>>>
First Name: <input type=text name=firstname size=15><br>
Last Name: <input type=text name=lastname size=15><br>
Gender: <select name=gender><option value="M">Male<option
value="F">Female</select>
</form>
</html>

Before the Web page is run the variables have the default value ERROR, so when the page is run they are set to initial values as specified in the IF-THEN statement at the top of the file. The HTML form takes on the values of those variables. When the HTML page is provided to the user the values appear in the HTML form. The page transmitted to the browser contains the initial values even though you never needed to program them into your HTML form. Using the "View Source" option of your browser after running this page for the first time, you'll see the NAME-VALUE pairs in the document.

The automatic filling of HTML form elements gives you the ability to integrate HTML forms into the applications you write. It also allows you to span HTML forms across multiple documents.

For example, suppose you have a lengthy form that would be better off spread across multiple documents. Then divide the form into multiple forms, placing each of the smaller forms on a single page. Then add next and previous HREF links to the pages to give your user the ability to navigate between the forms. The automatic filling of HTML forms by H2O will ensure the forms still retain their information and behave like an HTML form placed on a single page.

Specifying Values in HTML Forms
It is a good habit to avoid setting VALUEs in forms explicitly. If you need to place a value in a FORM element, set the value prior to displaying the form. If you do specify a VALUE in a form then the HTML form will show the VALUE as expected. But H2O will not take on the specified by the VALUE until the form is submitted.

Working with Delimited Text Files
Delimited Text Files are text files containing data organized in columns and rows. Most often, the files are exported from databases and spreadsheet programs. The character used to separate one cell from the next is called the delimiter. End-of-line characters are used to separate one row from the next. The example below is a comma delimited text file containing the state codes and names of five US states.

"CA","California"
"NY","New York"
"WA","Washington"
"VA","Virginia"
"OR","Oregon"

Almost all database programs and spreadsheets give you the ability to export your data in some sort of delimited text file. Most often the delimiter is a comma or a tab character.

Sometimes the cells of each row are surrounded with quote marks. Sometimes not. To load a delimited text file into a variable use COPY. For example:

<< COPY FILE="mystates.txt" TS="," TO mydata /COPY >>

The Overlay above will load the five states into mydata, creating a two column by five row variable. The TS parameter (TS stands for Table Separation) is a comma which tells H2O the file is comma-delimited.

If the TS parameter is not set then the file will be loaded into mydata like any other text file.

The text in the file will be placed into a single column by single row variable. It is important to remember the TS parameter when working with delimited text files.

The COPY tag can also be used to create delimited files. For example, suppose you wish to load the comma delimited file above and use it to create a new delimited file that's TAB
delimited. You could write:

<<
COPY FILE="mystates.txt" TS="," TO mydata /COPY >>
COPY mydata TO FILE="mystates2.txt" TS=TAB /COPY >>
>>

The first COPY instruction loads the comma-delimited text file into mydata. The second takes the variable and saves it to the file mystates2.txt.

The TS Parameter
The TS parameter can contain between one and three characters. It can contain the delimiter, the delimiter and a quoting character or a delimiter, quoting character and an
"escape" character. Most often only a delimiter needs to be defined, as shown above. In special cases other characters can be added. The nuances of how to deal with special
cases is beyond the scope of this book. For further information access the Knowledge Base on Aestiva's Web site.

Working with Loops
Anyone familiar with high-school BASIC is familiar with WHILE and FOR loops. The same kinds of loops work in H2O too. For example, suppose you want to write a small program
that copies files in a folder to a backup folder.

You could use a WHILE loop that starts with a list of files and copies them one at a time to the backup folder, each time deleting a file off of the list of files that need to be copied, until no files are left. The Overlay could be written as follows:

<<
myfilelist = FILELIST("/folder1")
myfile_in = "/folder1/" + myfilelist[1]
myfile_out ="/folder2/" + myfilelist[1]
WHILE ISFILE(myfile_in) DO
COPY FILE=myfile_in To FILE=myfile_out /COPY
myfilelist=DELROW(myfilelist,1)
myfile_in = "/folder1/" + myfilelist[1]
myfile_out ="/folder2/" + myfilelist[1]
/WHILE
>>

The first statement uses the tag FILELIST to place a list of file names found in "/folder1" into myfilelist. The next two instructions build file names for input and output files respectively. The WHILE statement is set to run provided an input file exists.

In the WHILE loop the COPY tag is used to copy the file after which the tag DELROW is
used to delete the first row of myfilelist. myfile_in and myfile_out are set again and the loop is repeated.

Another way to perform the operation above is to use a FOR loop. Normally a programmer would not use a FOR loop but FOR loops in H2O can loop automatically across variables.

Programmers, when using H2O, rately use WHILE loops. They find FOR loops more
useful and easier.

Standard FOR loops, like you find in high-school BASIC, are also not that popular. The standard way to use a FOR loop is to write.

FOR NAME=variable_name VALUE=begin_value TO end_value DO
instructions here
/FOR

You need to determine begin and end points which can lead to errrors. The more popular FOR loop in H2O is written as follows:

FOR NAME=variable_name ROWNAME=row_name DO
instructions here
/FOR

No begin or end values are needed since the loop knows to run once for each row of variable_name. Each time it loops it places in row_name the next row of variable_name. The first time in the loop row_name is set a one row variable taken from the first row of variable_name. The second time through the loop row_name is set to a one row variable taken from the second row of variable_name, and so on. Rewriting the example above with a FOR loop you get:

<<
myfilelist = FILELIST("/folder1")
FOR NAME=myfilelist ROWNAME=myfile DO
myfile_in = "/folder1/" + myfile[1]
myfile_out ="/folder2/" + myfile[1]
COPY FILE=myfile_in To FILE=myfile_out /COPY
/WHILE
>>

The example above used FILELIST to fill a two dimensional variable called myfilelist. The FOR loop fills myfile, each time through the loop, with the next row in myfilelist allowing one to specify the files that need to be copied and copy them.

Home | Cart | Free Download | Online Manual
COPYRIGHT © 2005 Aestiva, LLC. ALL RIGHTS RESERVED.