Lua Programming Beginners Learn Lua Programming Step By Step Very Easy Rizk instant download
Lua Programming Beginners Learn Lua Programming Step By Step Very Easy Rizk instant download
https://ebookbell.com/product/lua-programming-beginners-learn-
lua-programming-step-by-step-very-easy-rizk-37070252
Lua Programming Beginners Learn Lua Programming Step By Step Very Easy
Rizk
https://ebookbell.com/product/lua-programming-beginners-learn-lua-
programming-step-by-step-very-easy-rizk-232145960
https://ebookbell.com/product/lua-programming-gems-illustrated-luiz-
henrique-de-figueiredo-54831260
https://ebookbell.com/product/beginning-lua-programming-1st-edition-
jung-kurt-55199762
https://ebookbell.com/product/beginning-lua-programming-1st-edition-
kurt-jung-aaron-brown-5468708
Beginning Lua Programming Kurt Jung Aaron Brown
https://ebookbell.com/product/beginning-lua-programming-kurt-jung-
aaron-brown-59266488
https://ebookbell.com/product/lve-for-lua-game-programming-1st-
edition-akinlaja-darmie-43150088
https://ebookbell.com/product/programming-in-lua-4th-edition-roberto-
ierusalimschy-49119332
https://ebookbell.com/product/programming-in-lua-2nd-edition-roberto-
ierusalimschy-23299570
https://ebookbell.com/product/programming-in-lua-3rd-edition-roberto-
ierusalimschy-4415838
By Claudia Alves
& Alexander Aronowitz
TABLE OF
CONTENTS
Introduction The audience
About the third edition Other
resources Certain
Typographical Conventions
Running examples
Acknowledgments PART I
Language Chapter 1. Getting
Started 1.1. Blocks 1.2. Some
lexical conventions 1.3. Global
variables 1.4. Separate
interpreter
Chapter 2. Types and values
2.1. Nil 2.2. Boolean 2.3.
Numbers 2.4. Strings
Literals
Long lines
Type conversions
Exercises
Begin Chapter 1
% lua hello.lua
1.1. Blocks
Every piece of code that Lua executes, such
as a file or from- a smart string in interactive
mode is called a chunk. A block is simply a
sequence of commands (or statements). Lua
does not need a separator between
consecutive operators, but you can use
semicolon if you like. I personally use I use a
semicolon only to separate statements written
in one line. Line splitting does not play any
role in the syntax system Lua; so, the
following four blocks are valid and
equivalent:
a=1
b=a*2
a = 1;
b = a * 2;
a = 1; b = a * 2
a = 1 b = a * 2 - ugly, but valid
% lua -i prog
ij i10 _ij
aSomewhatLongName _INPUT
and break do
else elseif
end false goto for function
if in
local nil not
or repeat return then true
until while
- [[
print (10) - no action (commented out)
-]]
--- [[
print (10) -> 10
-]]
b = nil
print (b) -> nil
1.4. Separate
interpreter
Stand-alone interpreter (also called lua.c in
relation to the name of its source file or just
lua by name executable file) is a small
program that allows direct use of Lua. This
section presents her basic options.
When the interpreter loads a file, it skips the
first string if it starts with '#' character . This
allows you to use make Lua a scripting
interpreter on UNIX systems. If you start
your script with something like
#! / usr / local / bin / lua
Exercises
Exercise 1.1 . Run the factorial example.
What will happen
with your program if you enter a negative
number?
Modify the example to avoid this problem.
Exercise 1.2 . Run the twice example by
loading once
file using the –l option , and another time
using dofile . what
faster?
Exercise 1.3 . Can you name another
language using
(-) for comments?
Exercise 1.4 . Which of the following lines
are valid
by our identifiers?
___ _end End end until? nil NULL
CHAPTER 2 Types
and values
Lua is a dynamically typed language. The
language has no definitions of ty-pov, each
value carries its own type.
There are eight basic types in Lua: nil ,
boolean , number , string , userdata , function
, thread and table . The type function returns
the type for any passed value:
2.1. Nil
Nil is a type with only one value, nil , the
main which is different from all other values.
Lua
uses nil to indicate a missing value. Like us
already seen, globals default to nil before its
first assignment, you can also assign nil a
global variable to delete it.
2.2. Boolean (boolean
values)
The boolean type has two values, true and
false , which serve to pre- setting traditional
logical values. However, these values do not
monopolize all conditional values: in Lua,
any value can represent a condition. Relevant
checks (checking conditions in various
control structures) interpret both nil and false
as false and all other values as valid muddy.
In particular, Lua treats zero and an empty
string as true. in logical conditions.
Throughout the book, false will mean nil. and
false . In the case when it is about boolean
values, there will be explicitly set to false .
2.3. Numbers
The number type represents floating point
values specified with double precision. Lua
does not have a built-in integer type.
Some fear that even simple operations such
as increase by one (increment) and
comparison, may be incorrect work with
floating point numbers. However, in fact it
isnot this way. Almost all platforms now
support the standard IEEE 754 for floating
point representation. According to this
standard, the only possible source of errors is
There is a case where the number cannot be
accurately represented. Opera- walkie-talkie
rounds its result only if the result cannot be
accurately represented as the corresponding
float value point. Any operation whose result
can be accurately predicted put, will have the
exact meaning. In fact, any integer up to 2 53
(approximately 10 16 ) has an exact floating
point representation with double precision.
When you use a float value doubleprecision
dot to represent integers, no rounding errors
unless the value is greater than 2 5353 bit
integer values without rounding problems. Of
course, fractional numbers will have rounding
problems. This the situation is no different
than when you have paper and a pen. If we
want to write 1/7 in decimal form, then we
must somewhere us stop. If we use ten digits
to represent number, then 1/7 becomes
0.142857142 . If we calculate 1/7 * 7 s ten digits,
we get 0.999999994 , which is different from 1.
Moreover, numbers that have a finite
representation in the form decimal fractions
can have an infinite representation in the
form binary fractions. So, 12.7-20 + 7.3 is not
zero, since both numbers 12.7 and 7.3 do not
have an exact binary representation (see
Figure 2.3). Before we continue, remember
that integers have an exact representation and
therefore have no rounding errors. Most
modern CPUs perform floating operations.
dot as fast (or even faster) than with integers.
However, it is easy to compile Lua so that for
numeric values a different type was used, for
example long integers single-precision
floating-point values or numbers. It especially
useful for platforms without hardware
support for numbers with floating point such
as embedded systems. For de- For this, refer
to the luaconf.h file in the Lua source files. We
can write numbers, if necessary, specifying a
fraction part and decimal degree. Examples
of valid numeric constants are:
4 0.4 4.57e-3 0.3e12 5E + 20
2.4. Strings
Lua strings have the usual meaning: a
sequence of characters fishing. Lua supports
all 8-bit characters, and strings can be keep
characters with any codes, including zeros. It
means that you can store any binary data as
strings. you also you can store unicode strings
in any representation (UTF-8, UTF-16, etc.).
The standard library that comes with Lua is
does not contain built-in support for these
views. However less, you may well be
working with UTF-8 strings, which we will
consider Rome in section 21.7. Lua strings
are immutable values. You can not change
the character inside the string, as you can in
C; instead of this you create a new line with
the desired changes as shown in following
example:
a = “one string”
b = string.gsub (a, “one”, “another”) - change part of the
string
print (a) -> one string
print (b) -> another string
a = “hello”
print (#a) -> 5
print (# "good \ 0bye") -> 8
Literals
Long lines
We can delimit character strings with double
square brackets, as we did with the
comments. Line in this form can span many
lines, and control sequences the numbers on
these lines will not be interpreted. Moreover,
this the form ignores the first character of the
line if it is a jump character next line. This
form is especially useful for writing lines,
containing large snippets of code as shown
below:
page = [[
<html>
<head>
<title> An HTML Page </title>
</head>
<body>
<a href=32http://www.lua.org> Lua </a>
</body>
</html>
]]
write (page)
Type casts
Lua provides automatic conversion of values
between
strings and numbers at run time. Any numeric
operation
applied to a string, tries to convert the string
to a number:
line = io.read ()
- read the line
n = tonumber (line)
- try to translate it into a number
if n == nil then
error (line .. “is not a valid number”)
else
print (n * 2)
end
2.5. Tables
The table type corresponds to an associative
array. Associative a ny array is an array that
can be indexed not only numbers, but also
strings or any other value from the language,
except for nil .
Tables are the main (actually the only) place
a hanism for structuring data in Lua, and very
powerful. We use tables to represent regular
arrays, sets, records and other data structures
simple, homogeneous and in an efficient way.
Lua also uses tables to pre- placing packages
and objects. When we write io.read , we think-
This is about the “ read function from the io
module ”. For Lua, this expression means
"Take the value from the io table by the read
key . " Lua tables are neither values nor
variables; they objects . If you are familiar
with arrays in Java or Scheme, then you
should you know what I mean. You can think
of a table as dynamically allocated object;
your program only works with a link
(pointer) to it. Lua never uses covert copying
or creating new tables. Moreover, you even
no need to declare a table in Lua; in fact there
is not even a way declare a table. You create
tables using a special expression, which in the
simplest case looks like {} ;
a = {}
- create a table and remember the link to it in 'a'
k = “x”
a [k] = 10
- new record with key "x" and value 10
a [20] = “great” –- new record with key 20 and value
“great”
print (a [“x”]) -> 10
k = 20
print (a [k]) -> “great”
a [“x”] = a [“x”] + 1 –- increase the record “x”
print (a [“x”]) -> 11
a [“x”] = 10
b=a
- 'b' refers to the same table as 'a'
print (b [“x”]) -> 10
b [“x”] = 20
print (a [“x”]) -> 20
a = nil - only 'b' still refers to the table
b = nil - there are no references to the table
a = {}
- empty table
- create 1000 new entries
for i = 1, 1000 do a [i] = i * 2 end
print (a [9]) -> 18
a [“x”] = 10
print (a [“x”]) -> 10
print (a [“y”]) -> nil
ax = 10 - same as a [“x”] = 10
print (ax) - same as print (a [“x”])
print (ay) - same as print (a [“y”])
a [i] = io.read ()
end
end
ebookbell.com