Hamster-Scripts: Functions (built-in)

Variables

set( <variable>, <expression> )

<variable> = <expression>

Sets the given <variable> to the result of the <expression>.

The second, more readable form, is only allowed at the beginning of a line, as "=" would be interpreted as the comparison-operator if used in expressions.

Returns: Result of expression, i.e. the value the variable was given.

var( $a )
set( $a, 42 )
$a = 42

inc( <variable>, <count> )

inc( <variable> )

dec( <variable>, <count> )

dec( <variable> )

Increments ("inc") or decrements ("dec") the given variable by 1 or by the given <count>.

Returns: New value of variable.

inc( $a )
dec( $a, 2 )

var( <variable> [ , <variable> ... ] )

Declares the given variables before they can be used. The initial value of new variables is undefined, already existing ones are reset to an undefined state. See also: "varset"

Returns: Number of variables.

var( $a, $b, $c )

varset( <variable1>, [ <variable2>, ..., ] <expression> )

Declares the given <variables> like "var" and sets their inital value to the result of <expression>.

Returns: Result of expression, i. e. the value the variables were given.

VarSet( $a, $b, $c, 42 )

IsVar( <variable> )

Checks, if the given variable exists. See "sub" for its purpose.

Returns: 1 (=true), if variable exists, 0 (=false) otherwise

sub Test( *$optionalRefVar? )
   if( IsVar( $optionalRefVar ) )
      $optionalRefVar = 42
   endif
   # ...
endsub

IsVarSet( <variable> )

Checks, if the given variable is assigned a valid value. See "sub" for its purpose.

Returns: 1 (=true), if variable has a valid value, 0 (=false) otherwise

sub Test( $optionalVar? )
   if( !IsVarSet( $optionalVar ) )
      $optionalVar = 42
   endif
   # ...
endsub

const( <variable>, <expression> )

Declares the given <variable> as a constant and sets their inital value to the result of <expression>.

Constants can be used like variables but their values can't be changed afterwards.

Returns: Result of expression, i. e. the value the variable was given.

Const( $Answer, 42 )

constenum( <variable0>, [ <variable1>, ..., ] )

Declares the given <variables> as constants like "const" and sets their inital value to the position in the list. First variable is set to 0, second to 1 and so on.

Returns: Value of last constant.

ConstEnum( $Option0, $Option1, $Option2, $Option3 )

dump( <withconst> )

dump

Display all variables with their values for testing-purposes.

If <withconst> is given and true, constants are also shown.

Returns: 0.

Dump( True )
Dump

entercontext

leavecontext

A "context" defines the scope for which variables are valid. If a new context is created (entercontext), all variables created in this context are local to it, and if the context is left (leavecontext), they will be deleted. You never should need these statements, as they are automatically invoked while a "sub" is executed (i.e. each sub has its own, local variables).

Returns: New context ID (Integer).

var( $a )
$a = 42
entercontext
var( $a )
$a = 999
leavecontext
print( $a ) # its value is still 42

[www.elbiah.de Hamster Playground Documentation]