11.11 Dim and DimStr statements

 
Dim <varname>[<dim1-size>] ([<dim2-size>], ...), ...
DimStr <varname>[<dim1-size>] ([<dim2-size>], ...), ...

The Dim and DimStr statements can be used to create a n-dimensional table with the specified sizes and initialize all its elements to 0 ( Dim ) or "" ( DimStr ).

As you already know from the documentation of the table data type, table fields need to be initialized before they can be used. Even if a field shall only carry a zero or an empty string, you have to initialize it with that value before you can access it. The Dim statement can help you here. It will create the table specified by varname with the size specified in size. The size parameter must be a constant value, not a variable. Please note, that size specifies really the table's size and not the last element that is to be initialized. Thus, if you use 50 as size, Hollywood will initialize table fields 0 to 49. Table field 50 will not be initialized.

Here is an example:

 
Dim mytable[100]

This statement translates to the following Hollywood code:

 
Local mytable = {}
For k = 0 To 99 Do mytable[k] = 0

The Dim statement comes really handy if you want to create multi-dimensional tables. You can use as many of the square brackets after the varname specification as you like. Each new square bracket will create a new table dimension of the specified size. For example:

 
Dim vector[10][10][10]

The statement above creates a three dimensional vector table and initializes it with all zeros. This statement translates to the following, quite a bit more complex, Hollywood code:

 
Local vector = {}
For i = 0 To 9
    vector[i] = {}
    For j = 0 To 9
       vector[i][j] = {}
       For k = 0 To 9
          vector[i][j][k] = 0
       Next
    Next
Next

You can also create and initialize more than one table with this statement. Just use a comma after the last dimension specification and you can repeat the whole procedure as many times as you like. Example:

 
Dim table1[50], table2[50], table3[50]

The DimStr statement works in the same way than Dim but initializes all fields with empty strings ("").

Please note that Dim / DimStr will always create local tables if you are not in the main block of your script. So if you want a table to be global, be sure to use the Dim / DimStr statement in the script's main block.

Keep also in mind that Dim / DimStr do not limit the table to the specified size. The table can still grow because Hollywood is a dynamically typed language! To grow a table, simply initialize the fields you need and Hollywood will automatically grow it. For example:

 
Dim table[50]
...
For k = 50 To 59 Do table[k] = 0   ; grow table by 10 fields

The code above creates a table with space for 50 fields and grows it to 60 fields then. If you want to shrink a table, set the corresponding fields to Nil. For instance:

 
Dim table[50]
...
For k = 40 To 49 Do table[k] = Nil ; shrink table by 10 fields

The code above shrinks the table from 50 initialized fields to 40 initialized fields. Hollywood is a dynamically typed language in which tables do not have a fixed size. You simply grow and shrink your tables as you need it.


Show TOC