Lua Logo
Basic Functions



assert(condition[, cessage])
collectgarbage ([limit])
dofile(filename)
error( message[, level] )
gcinfo()
_G
getfenv (f)
ipairs (table)
loadfile(filename)
loadstring (string [, chunkname])
next(table[, index])
pairs (table)
pcall (myFunction, parameter1, oarameter2, ...)
print (e1, e2, ...)
rawequal (v1, v2)
rawget (table, index)
rawset (table, index, value)
require (packagename)
setfenv (f, table)
setmetatable (table, metatable)
tonumber (e [, base])
tostring (e)
type( variable )
unpack (list)
_VERSION
xpcall (f, err)

nach obenassert(condition[, message])

As soon as the condition will be false or nil, the programm will be stopped and returns the message.

The message is like:

filename:lineNo:message

Stack traceback:

Afterwards we get information where the problem is located.

example:
    
    assert( 1 == 2"That is wrong" )
    
    --> filename:lineNo:That is wrong
    
    

nach obencollectgarbage ([limit])

The garbage collector.

As the name already tells, with this function the garbage collector of the programm can be started. The limit specifies the size of the reserved memory of the cargabe collector.
The garbage collector is cleaning the memory of occupied space. To specify the limit is optional. If no limit is specified, the function takes 0.

Example:
    
    print( gcinfo()) --> 17 29
    bigString = string.rep('a'100000-- a lot of memory will be used
    print( gcinfo()) --> 322 197
    collectgarbage (10000)
    print( gcinfo()) --> 163 10000 apparently it has not been used as much memory as we thought. The limit is set to 10000.
    collectgarbage (5)
    print( gcinfo()) --> 139 279 something has been found, so the limit of 5 we set, seems to be too small.
    bigString = nil -- the value of bigString is not available anymore. We have been deleted it.
    collectgarbage ()
    print( gcinfo()) --> 29 59
    

!!!!!!!!! Since lua 5.1 collectgarbage("count") has been replaced the function gcinfo() !!!!!!!!!!


nach obendofile(filename)

Opens the file named dateiName and is executing it at Lua-code.

Return: what the file is returning as result of file code.

With this function we will be able to split our project into any separate files. It seems as it has been writen in one pour.

Example:

File 1 (Unit1.lua)
    
    return "I am File 1"
    
File 2 (Unit2.lua) <-- wird ausgeführt
    
    print( dofile("Unit1.lua") ) --> I am File 1
    

nach obenerror( message[, level] )

The function error returns the bug which has been produced by the last executed code in subject to the value of level and aborts the programm.
To explain the parameter level I will show you the following function:
     
     1   function IFoundTheBug()
     2       a = ""
     3       print( a..b ) -- b is nil therefore error
     4   end
     5
     6   function myError()
     7       _, cError = pcall(IFoundTheBug) -- at this line the function will be executed with pcall (protected)
     8   end
     9
    10   function Call_1()
    11       error(cError,1-- Error-message will be returned and the programm will be aborted.
    12   end
    13   
    14   function Call_2()  -- a kind of intermediate station
    15      Call_1()
    16   end
    17   myError() -- call of function, that creates the error in a protected environment
    18   
    19   Call_2() -- call of function that returns the error by using the intermediate station
    

What happens?

Now the results:

level = 0 --> the error/bug will be returned promptly.

next

level = 1 --> a level before

next

level = 2 --> again a level before

next

level = 3 --> 3 levels before

next


nach obengcinfo()

This function returns 2 values.

garbage collector is a independent working programm, that clears memory that will not be used anymore. See also collectgarbage

Both values are in kB.

!!!!!!!!! Since lua 5.1 collectgarbage("count") has been replaced this function !!!!!!!!!!

Example:
    
    print( gcinfo()) --> 17 29
    bigString = string.rep('a'100000-- a lot of memory will be used
    print( gcinfo()) --> 322 197
    

nach obennext(table[, index])

The function next returns index after index and each associated value of table in an unsorted way. At index == nil next returns the first index. If the last index will be reached, next returns nil.

Example:

next

Felt(Adapted) function inext:
        
    function inext(_table, _index)
        _index = next(_table, _index)
        if _index then
            if type(_index) ~= "number" then
                repeat
                    _index = next(_table, _index)
                    if type ( _index) == "number" then
                        break
                    end
                until not _index
            end
        end
        return _index
    end
    

nach obenpairs (table)

The only task of the function pairs is, to execute the function next and to return the according table.

    
    myFunc, myTable = pairs( table )
    
myFunc includes the function next
and in myTable the returned table.

We can find this in the generic for-loop

nach obenipairs (table)

The only task of the function ipairs is like next and to return the according table.
Supposed the function inext would exist (similar to the function next at pairs) and it would return following:
    
    myFunc, myTable = ipairs( table )
    

Now there is a function existing in myFunc similiar to next. However, it only returns its next numerical index and returns the assigned table to myTable.

We can find this in the generic for-loop

nach obenloadfile(filename)

Opens the file filename and executes it, but not as Lua-code.

Returns 2 values:

Example:
    
    loadedFunction, cError = loadfile("filename.lua")
    

nach obenpcall (myFunction, parameter1, parameter2, ...)

The function pcall executes the function myFunction with the according parameters.

The difference to the regular call: It will not be responded directly on an error.

The function returns following:


Example:

pcall

The programm aborts and you will receive the error-message.

nach obenrawget (table, index)

Gets the real value of table[index], without invoking any metamethod.

Parameter:


    
    result = rawget (myTable, 5)
    

Can be compared with
    
    result = myTable[5]
    

nach obenrawset (table, index, value)

Sets the value of table[index], without invoking any metamethod.


Parameter:


    
    rawget (myTable, 5true)
    

Can be compared with

    
    myTable[5] = true
    

nach obentonumber (e [, base])

Changes the variable into a number to the base of 10 logarithm. If this will not succeed the result is nil.

Parameter:

The base can be any even value between 2 and 36.

Can be explained best of all with an example:

    
    print(tonumber("55"))          --> 55
    print(tonumber(55))            --> 55
    print(tonumber("55a"))         --> nil
    print(tonumber(10101.1012e12)) --> 1.01011012e+16
    print(tonumber("100110"), 2)   --> 38
    print(tonumber("100112"), 2)   --> nil
    print(tonumber("LUA"), 36)      --> 28306
    


nach obentype( variable )

This function returns a string with a type of an variable.

As parameters we have to give over following:

    
    type (variable)
    
possibel returns:

"nil, "number", "string", "boolean, "table", "function", "thread", und "userdata".


Example:
    
    result = type (5)
                --> resultat == "number"
    
    myVar = "This is a sentence"
    result = type ( myVar )
                --> result == "string"
    

Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006