Curl/Example 3

From Wikibooks, open books for an open world
< Curl
Jump to: navigation, search
{curl 5.0, 6.0 applet}

|| A procedural example

{value
    let my-table:Table = {Table}

    {my-table.add {row-prototype "Number", "Square", "Cube"}}
   
    {for i:int = 1 to 25 do
        let square:int = i*i
        let cube:int = square*i
        {my-table.add {row-prototype i, square, cube}}
    }

    my-table
}

The above example shows some simple procedural code to create a table with the numbers 1 - 25 with Square and Cube Values. Lines 1-3 are as described above.

The 4th line starts a Value Block - this is a mechanism to hide some level of code within a Scope and to Return a Value - in this case to display in the web page. (Curl also has a do block that does not return anything)

Line 5 loads the row that holds the header information into the table.

Line 7 defines a new variable - my-table, of type Table and assigns it an initial value, the {Table} part of the line creates a new instance of Table. Curl does support an any type but its use is slower as the system has to make runtime checks on the type.

Line 9 creates a for iterator with a locally scoped variable i of type integer. Line 10 defines a local variable square and line 11 does the same for cube.

line 12 adds a new row to the table with the values of i, square and cube.

Line 13 ends the for construct.

Line 15 has the name of my-table so the value of my-table will be used as the result of the Value block.

Line 16 ends the value block.