ARRAY.sml

  Download

More scripts: SML Fundamentals

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# ARRAY.SML
#
# Sample script for tutorial "Writing Scripts with SML"
# Use of arrays, matrices, and stringlists.
clear();
### Declare a one-dimensional array with 5 items.
array numeric testarray[5];
numeric i;
numeric x = 100;
### Loop through array to set values and print to console.
### NOTE: Array indices begin with 1.
print( "Array Status:" );
for i = 1 to 5 {
	testarray[i] = x;
	x += 100;
	printf( "Array index %d = %d\n", i, testarray[i] );
	}
### Create a matrix with 1 row and 5 columns.
class MATRIX testmatrix;
testmatrix = CreateMatrix( 1, 5 );
### Loop through row 1, set column value, then get value to print to console.
### NOTE: Matrix row/column indices begin with 0.
printf( "\nMatrix Row 0 Status:\n");
for i = 0 to 4 {
	SetMatrixItem( testmatrix, 0, i, testarray[i + 1] ); 
	printf( "Column %d= %d\n", i, GetMatrixItem( testmatrix, 0, i ) );
	}
### Declare a STRINGLIST 
class STRINGLIST stringlist;
string str1$, str2$, str3$;
str1$ = "ten";
str2$ = "twenty";
str3$ = "thirty"; 
### Add strings to end of stringlist.  You can also add
### strings to the front of the list.
stringlist.AddToEnd(str1$);
stringlist.AddToEnd(str2$);
stringlist.AddToEnd(str3$);
printf( "\nStringlist Status:\n" );
printf( "Number of items = %d\n", stringlist.GetNumItems() );
### Loop through stringlist and use GetString() method to
### get string values and print to console.
### NOTE: stringlist indices begin with 0.
for i = 0 to 2 {
	printf( "Index %d = %s\n", i, stringlist.GetString( i ) );
	}
### You can also use array subscripts on a STRINGLIST.
### Repeat above loop using array subscripts:
printf( "\nStringlist Status Again:\n" );
for i = 0 to 2 {
	printf( "Index %d = %s\n", i, stringlist[i] );
	}