|
| 1 | +-- ----------------------------------------------------------------------------- |
| 2 | +print( "----- Strings" ) |
| 3 | +-- ----------------------------------------------------------------------------- |
| 4 | + |
| 5 | +-- The standard Lua library 'string' supports nice convenient functions for working with strings: |
| 6 | +print( string.byte( "ABCDE", 1, -2 ) ) -- console output: "65 66 67 68" |
| 7 | +print( string.rep( "w", 3 ) .. ".sick.com" ) -- console output: "www.sick.com" |
| 8 | +print( string.reverse( "Hello" ) ) -- console output: "olleH" |
| 9 | +print( string.sub( "AppSpace", 6, -1 ) ) -- console output: "ace" |
| 10 | +print( string.format( "%s %.10f", "Pi is exactly", math.pi ) ) |
| 11 | + -- console output: "Pi is exactly 3.1415926536" |
| 12 | + |
| 13 | +-- ----------------------------------------------------------------------------- |
| 14 | +print( "----- Concatenate tables and access elements" ) |
| 15 | +-- ----------------------------------------------------------------------------- |
| 16 | + |
| 17 | +-- The standard Lua library 'table' offers a mighty set of functionality. Concatenating tables is very efficient in Lua: |
| 18 | +local myTable = { "one", "two", "three", "four" } |
| 19 | +print( table.concat( myTable ) ) -- console output: "onetwothreefour" |
| 20 | +print( table.concat( myTable, ":" ) ) -- console output: "one:two:three:four" |
| 21 | +print( table.concat( myTable, "-", 2, 4 ) ) -- console output: "two-three-four" |
| 22 | +print( table.maxn( myTable ) .. " == " .. #myTable ) -- console output: "4.0 == 4" |
| 23 | +-- Append an item, indices should be successive without gaps and start with 1, not 0: |
| 24 | +myTable[ #myTable + 1 ] = "five" |
| 25 | +table.insert( myTable, #myTable + 1, "six" ) |
| 26 | +--table.insert( myTable, 9, "nine" ) |
| 27 | + -- console output: "Runtime error: bad argument #2 to 'insert' (position out of bounds)" |
| 28 | +-- Appending a 'nil' item will not be considered: |
| 29 | +table.insert( myTable, #myTable + 1, nil ) |
| 30 | +myTable[ #myTable + 1 ] = nil |
| 31 | +table.insert( myTable, #myTable + 1, "nine" ) |
| 32 | +print( #myTable .. "; " .. myTable[ #myTable ] ) -- console output: "7; nine" |
| 33 | +-- Sort with inline function as sorting method: |
| 34 | +table.sort( myTable, function ( a, b ) return a < b end ) |
| 35 | +print( table.concat( myTable, "," ) ) |
| 36 | + -- console output "five,four,nine,one,six,three,two" |
| 37 | + |
| 38 | +-- ----------------------------------------------------------------------------- |
| 39 | +print( "----- Use tables as dictionaries or maps and access elements" ) |
| 40 | +-- ----------------------------------------------------------------------------- |
| 41 | + |
| 42 | +local myMap = { key1 = "value1", key2 = false } |
| 43 | +print( myMap.key1 ) -- console output: "value1" |
| 44 | +-- Add a new key/value pair, value can also be a table: |
| 45 | +myMap.newKey = { "one", "two", "three" } |
| 46 | +-- Remove 'key2' from table: |
| 47 | +myMap.key2 = nil |
| 48 | +-- Use '.' to reach certain values: |
| 49 | +print( myMap.key1 ) -- console output: "value1" |
| 50 | +print( myMap.key2 ) -- console output: "nil" |
| 51 | +-- Alternatively, use square brackets to reach certain values: |
| 52 | +print( myMap[ "newKey" ][ 2 ] ) -- console output: "two" |
| 53 | +-- Some table operations do not work with dictionaries or maps: |
| 54 | +print( #myMap ) -- console output: "0" |
| 55 | +print( #myMap.newKey ) -- console output: "3" |
| 56 | +-- Lua will not check for the range of available indices: |
| 57 | +print( myMap.newKey[ 10 ] ) -- console output: "nil" |
| 58 | + |
| 59 | +-- ----------------------------------------------------------------------------- |
| 60 | +print( "----- Library math" ) |
| 61 | +-- ----------------------------------------------------------------------------- |
| 62 | + |
| 63 | +-- The standard Lua library 'math' provides a large function set. Here is just a small excerpt: |
| 64 | +print( math.sin( math.pi / 2 ) ) -- console output: "1.0" |
| 65 | +print( string.format( "%f", math.sin( math.pi * 0.5 ) ) ) |
| 66 | + -- console output: "1.000000" |
| 67 | +print( math.min( 56, 4, 76, 23, 2, 76 ) ) -- console output: "2" |
| 68 | +local myNumTable = { 15, -4, 76, 32, 10, 21, -76, 63 } |
| 69 | +table.sort( myNumTable, function ( a, b ) return math.abs( a ) < math.abs( b ) end ) |
| 70 | +print( math.abs( myNumTable[ 1 ] ) ) -- console output: "4" |
| 71 | +local myNumber = 2 ^ 8 - 1 |
| 72 | +local fraction, exponent = math.frexp( myNumber ) |
| 73 | +print( fraction .. " * 2 ^ " .. exponent .. " = " .. fraction .. string.format( " * %i = %i", 2 ^ exponent, myNumber ) ) |
| 74 | + -- console output: "0.99609375 * 2 ^ 8 = 0.99609375 * 256 = 255" |
| 75 | + |
| 76 | +-- Try more using the standard Lua library 'math': |
| 77 | + |
| 78 | +-- ----------------------------------------------------------------------------- |
| 79 | +print( "----- if and inline condition" ) |
| 80 | +-- ----------------------------------------------------------------------------- |
| 81 | + |
| 82 | +local a, b = 5, 5 |
| 83 | +if ( a == b ) then |
| 84 | + print( a .. " is equal to " .. b ) -- console output: "5 is equal to 5" |
| 85 | +elseif ( a > b ) then |
| 86 | + print( a .. " is greater than " .. b ) -- console output: "5 is greater than 5" |
| 87 | +else |
| 88 | + print( a .. " is less than " .. b ) -- console output: "5 is less than 5" |
| 89 | +end |
| 90 | +local c, d = "blue", "red" |
| 91 | +if ( c ~= d ) then |
| 92 | + print( c .. " is not " .. d ) -- console output: "blue is not red" |
| 93 | +else |
| 94 | + print( c .. " is " .. d ) -- console output: "blue is red" |
| 95 | +end |
| 96 | +local x, y = 12, 34 |
| 97 | +print( x .. ( x == y and " is equal to " or " is not equal to " ) .. y ) |
| 98 | + -- console output: "12 is not equal to 34" |
| 99 | + |
| 100 | +-- ----------------------------------------------------------------------------- |
| 101 | +print( "----- while loop" ) |
| 102 | +-- ----------------------------------------------------------------------------- |
| 103 | + |
| 104 | +a, b = 5, 1 |
| 105 | +-- If condition is false at all, block will not be executed: |
| 106 | +while ( a > b ) do |
| 107 | + print( a .. " is still greater than " .. b ) |
| 108 | + b = b + 1 |
| 109 | + -- Do not forget about the condition to avoid infinite loops! |
| 110 | +end |
| 111 | + |
| 112 | +-- ----------------------------------------------------------------------------- |
| 113 | +print( "----- for loop" ) |
| 114 | +-- ----------------------------------------------------------------------------- |
| 115 | + |
| 116 | +-- A numerical 'for' loop: |
| 117 | +local start, stop, step = 10, 0, -1 |
| 118 | +-- If start index exceeds stop index at all, i.e. even in case of decreasing step, block will not be executed: |
| 119 | +for i = start, stop, step do |
| 120 | + print( i .. ( ( i % 2 == 0 ) and " is even" or " is odd" ) ) |
| 121 | +end |
| 122 | + |
| 123 | +-- A 'for' loop based on an array, i.e. table with numeric indices: |
| 124 | +for i, v in ipairs( { 1, 4, 9, 16, 25 } ) do |
| 125 | + print( "Index is " .. i .. ", value is " .. v ) |
| 126 | +end |
| 127 | + |
| 128 | +-- A 'for' loop based on a generic map, i.e. table with string indices: |
| 129 | +local capitals = { Germany = "Berlin", France = "Paris", Norway = "Oslo", |
| 130 | +England = "London", Spain = "Madrid", Ukraine = "Kiew", Austria = "Vienna", |
| 131 | +Portugal = "Lisbon", Italy = "Rome", Croatia = "Zagreb" } |
| 132 | +print( "Some european countries and their capitals (unsorted):" ) |
| 133 | +for k, v in pairs( capitals ) do |
| 134 | + print( " - The capital of " .. string.format( "% 8s", k ) .. " is " .. v .. "." ) |
| 135 | +end |
| 136 | +table.sort( capitals ) |
| 137 | +print( "Some european countries and their capitals (sorted?):" ) |
| 138 | +for k, v in pairs( capitals ) do |
| 139 | + print( " - The capital of " .. string.format( "% 8s", k ) .. " is " .. v .. "." ) |
| 140 | +end |
| 141 | +-- Generic maps or dictionaries cannot be sorted, i.e. the 'pairs' iterator will jump unpredictable over the elements! |
| 142 | + |
| 143 | +-- ----------------------------------------------------------------------------- |
| 144 | +print( "----- repeat loop" ) |
| 145 | +-- ----------------------------------------------------------------------------- |
| 146 | + |
| 147 | +-- A 'repeat' loop: |
| 148 | +local i = 0 |
| 149 | +repeat |
| 150 | + print( "Counter is " .. i ) |
| 151 | + i = i + 1 |
| 152 | + -- Do not forget about the condition to avoid infinite loops! |
| 153 | +until i > 10 |
| 154 | +-- If the condition is false at all, the block will be executed at least once! |
0 commit comments