Chapter1 Lua 中的转义字符 and get start with Lua ,Types and Value

搞了这么多年programe, \n \t ,\\ \' \"" 就知道其转义,但\ddd \xhh 其实一直没搞明白,直到今天细细的读 programming in Lua 3rd englist version , I just get the meaning

pint("a"); , can als be write in print("\97"); or print("\x61");  / 97 61 其实是ASCII 数值

真可笑,,,


1.1 Chunks
Each piece of code that Lua executes, such as a file or a single line in interactive  mode, is called a chunk. A chunk is simply a sequence of commands (or
statements). , that mean .lua is a chunck, not mean in the .lua each statement is chunk, we can say that this is an excute unit.  in the interactive mode, a single statement

is a chunk.

You can use the -i option to instruct Lua to start an interactive session after
running the given chunk:
% lua -i prog

This is especially useful for debugging and manual testing.

Another way to run chunks is with the dofile function, which immediately
executes a file. For instance, suppose you have a file lib1.lua with the following
code:
function norm (x, y)
return (x^2 + y^2)^0.5
end
function twice (x)
return 2*x
end
Then, in interactive mode, you can type
> dofile("lib1.lua") -- load your library
> n = norm(3.4, 1.0)
> print(twice(n)) --> 7.0880180586677

The dofile function is useful also when you are testing a piece of code. You
can work with two windows: one is a text editor with your program (in a file
prog.lua, say) and the other is a console running Lua in interactive mode. After
saving a modification in your program, you execute dofile("prog.lua") in the
Lua console to load the new code; then you can exercise the new code, calling its
functions and printing the results.


The following words are reserved; we cannot use them as identifiers:
and break do else elseif  end false goto for function  if in local nil not or repeat return then true  until while

Lua is case-sensitive:

hyphen (--)  single line comment

--[[ multiple

line

comment]]


1.3 Global Variables

Global variables do not need declarations; you simply use them. It is not an
error to access a non-initialized variable; you just get the value nil as the result:
print(b) --> nil
b = 10
print(b) --> 10
If you assign nil to a global variable, Lua behaves as if the variable had never
been used:
b = nil
print(b) --> nil
After this assignment, Lua can eventually reclaim the memory used by the
variable.

1.4 The Stand-Alone Interpreter

The usage of lua is
lua [options] [script [args]]

Everything is optional

 -e option allows us to enter code directly into the command line, like here:
% lua -e "print(math.sin(12))" --> -0.53657291800043
(UNIX needs the double quotes to stop the shell from interpreting the parentheses.)


The -l option loads a library.

-i enters interactive mode after running the other arguments. Therefore, the next call will load the
lib library, then execute the assignment x=10, and finally present a prompt for
interaction.
% lua -i -llib -e "x = 10"

In interactive mode, you can print the value of any expression by writing a
line that starts with an equal sign followed by the expression:
> = math.sin(3) --> 0.14112000805987
> a = 30
> = a --> 30


Before running its arguments, the interpreter looks for an environment variable
named LUA_INIT_5_2 or else, if there is no such variable, LUA_INIT. If there
is one of these variables and its content is @filename, then the interpreter runs
the given file. If LUA_INIT_5_2 (or LUA_INIT) is defined but it does not start
with ‘@’, then the interpreter assumes that it contains Lua code and runs it.
LUA_INIT gives us great power when configuring the stand-alone interpreter,
because we have the full power of Lua in the configuration. We can preload
packages, change the path, define our own functions, rename or delete functions,
and so on.
A script can retrieve its arguments in the predefined global variable arg.
In a call like %lua script a b c, the interpreter creates the table arg with all

the command-line arguments, before running the script. The script name goes
into index 0, its first argument (“a” in the example) goes to index 1, and so on.
Preceding options go to negative indices, as they appear before the script. For
instance, consider this call:

% lua -e "sin=math.sin" script a b

The interpreter collects the arguments as follows:
arg[-3] = "lua"
arg[-2] = "-e"
arg[-1] = "sin=math.sin"
arg[0] = "script"
arg[1] = "a"
arg[2] = "b"


Lua is a dynamically typed language. There are no type definitions in the  language; 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 gives the type name of any given value:

print(type("Hello world")) --> string
print(type(10.4*3)) --> number
print(type(print)) --> function
print(type(type)) --> function
print(type(true)) --> boolean
print(type(nil)) --> nil
print(type(type(X))) --> string


2.1 Nil

Nil is a type with a single value, nil, whose main property is to be different from
any other value. Lua uses nil as a kind of non-value, to represent the absence
of a useful value. As we have seen, a global variable has a nil value by default,
before its first assignment, and you can assign nil to a global variable to delete
it.


2.2 Booleans
The boolean type has two values, false and true, which represent the traditional
boolean values. However, booleans do not hold a monopoly of condition values:
in Lua, any value can represent a condition. Conditional tests (e.g., conditions in
control structures) consider both the boolean false and nil as false and anything
else as true. In particular, Lua considers both zero and the empty string as true
in conditional tests. not the same as C++, in C++ , non 0 value is true, 0 is false

false and nil as false do remember this in lua

2.3 Numbers

The number type represents real (double-precision floating-point) numbers. Lua
has no integer type.

The fact is that any integer up to 2^53 (approximately 10^16) has an exact
representation as a double-precision floating-point number. When you use a
double to represent an integer, there is no rounding error at all, unless the

number has an absolute value greater than 2^53.


Before we go on, remember: integers do have exact representations and
therefore do not have rounding errors.

fractional numbers can have representation errors.

If we want to  write 1/7 in decimal, we will have to stop somewhere. (如果我们想把1/7 写成十进制形式,由于不时整数,小数位总有限制) If we use ten digits  to represent a number, 1/7 becomes rounded to 0.142857142. 取整到10数字, If we compute  1/7*7 using ten digits, the result will be 0:999999994, which is different from 1.

a=1/7*7;
print(a); //还是会print 1出来哦,,,


Moreover, numbers that have a finite representation in decimal can have an infinite representation in binary. (十进制上看是有限的,但二进制却有无限种表示形式) For instance, 12.7-20+7.3 is not exactly zero when computed with doubles, because both 12.7 and 7.3 do not have an exact  finite representation in binary

a=12.7-20+7.3 print(a);

//-8.8817841970013e-016   (not zero)

We can write numeric constants with an optional decimal part, plus an
optional decimal exponent. Examples of valid numeric constants are:
4  0.4  4.57e-3  0.3e12  5E+20   (指数表示形式,5 E的20次幂,注意E 并不时自然对数e=2.7182818, 而是10)

a=127E0.1; //error, E后不能使小数


做了这么多年还是今天仔细测试才清楚 ,so 1E1 =10, 1E2=100.

java 里面:double a=2e2; , 虽然我们知道 a是整数,但在java里面,E 只能用在double 里面,

int a=2E2, compile error.

float a=2e2f //need to subfix a f


cc=4.57e-3;
print(cc==4.57e-003); --true e-003 不管3前有多少个0,都不算开始的有效位

print(cc==4.57e-03); --true

print(cc==0.00457);--true





Moreover, we can also write hexadecimal constants, prefixing them with 0x.
Since Lua 5.2, hexadecimal constants can also have a fractional part and a
binary exponent (prefixed by ‘p’ or ‘P’),
as in the following examples:
0xff (255) 0x1A3 (419) 0x0.2 (0.125) 0x1p-1 (0.5 , 2的-1次幂, so 0x5p-1,. 5*2的-1次幂,也就是5*(1/2)=2.5)
0xa.bp2 (42.75 , 0xa.b is 10.6875 * 2^2 =42.75)
(For each constant, we added its decimal representation in parentheses.)










2.4 Strings

Strings in Lua are immutable values.  instead, you create a new string with the desired  modifications,

example:
a = "one string"
b = string.gsub(a, "one", "another") -- change string parts
print(a) --> one string
print(b) --> another string

Strings in Lua are subject to automatic memory management, like all other
Lua objects (tables, functions, etc.). This means that you do not have to worry
about allocation and deallocation of strings; Lua handles this for you. A string
can contain a single letter or an entire book. Programs that manipulate strings
with 100K or 1M characters are not unusual in Lua.
You can get the length of a string using the prefix operator ‘#’ (called the
length operator):
a = "hello"
print(#a) --> 5
print(#"good\0bye") --> 8

Literal strings
We can delimit literal strings by matching single or double quotes:
a = "a line"
b = 'another line'
They are equivalent; the only difference is that inside each kind of quote you can use the other quote without escapes. c=" haha ha 'no need to escape'  ppppp haahahaha ";


Long strings

page = [[
<html>
<head>
<title>An HTML Page</title>
</head>
<body>
<a href="http://www.lua.org">Lua</a>
</body>
</html>
]]
write(page)

Moreover, this form ignores the
first character of the string when this character is a newline.


The scanner  ignores pairs of brackets with a different number of equal signs. By choosing an
appropriate number of signs, you can enclose any literal string without having
to add escapes into it.

如果string 里面有]] 符号,那么就不能只是[[ ]] , 而是要加=号

page = [[
<html>
<head>
<title>An HTML Page</title>
</head>
<body>
<a href="http://www.lua.org">Lua</a>
</body>
</html>

a=b[c[i]];

]];//will hit error.


page = [==[
<html>
<head>
<title>An HTML Page</title>
</head>
<body>
<a href="http://www.lua.org">Lua</a>
</body>
</html>

a=b[c[i]];

]==];

This same facility is valid for comments, too. For instance, if you start a long
comment with --[=[, it extends until the next ]=]. This facility allows you easily
to comment out a piece of code that contains parts already commented out.

ie: --[===[

this is mutltiple line commen

a=b; --[[asfasf]]

]===]










For those situations, Lua 5.2 offers the escape sequence \z: it skips all
subsequent characters in the string until the first non-space character. The next
example illustrates its use:
data = "\x00\x01\x02\x03\x04\x05\x06\x07\z
\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"


Long strings are the ideal format to include literal text in your code, but you
should not use them for non-text literals. Although literal strings in Lua can

contain arbitrary characters, it is not a good idea to use them in your code: you
may have problems with your text editor; moreover, end-of-line sequences like
“\r\n” may change to “\n” when read. Instead, it is better to code arbitrary binary
data using numeric escape sequences, either in decimal or in hexadecimal,
such as “\x13\x01\xA1\xBB”.


Coercions

Lua provides automatic conversions between numbers and strings at run time.
Any numeric operation applied to a string tries to convert the string to a number:
print("10" + 1) --> 11
print("10 + 1") --> 10 + 1
print("-5.3e-10"*"2") --> -1.06e-09
print("hello" + 1) -- ERROR (cannot convert "hello")
Lua applies such coercions not only in arithmetic operators, but also in other
places that expect a number, such as the argument to math.sin.
Conversely, whenever Lua finds a number where it expects a string, it converts
the number to a string:
print(10 .. 20) --> 1020
(The .. is the string concatenation operator in Lua. When you write it right
after a numeral, you must separate them with a space; otherwise, Lua thinks
that the first dot is a decimal point.)


line = io.read() -- read a line
n = tonumber(line) -- try to convert it to a number
if n == nil then
error(line .. " is not a valid number")
else
print(n*2)
end


print(tostring(10) == "10") --> true  // tostring function
print(10 .. "" == "10") --> true
These conversions are always valid.


2.5 Tables

The table type implements associative arrays. An associative array is an array
that can be indexed not only with numbers, but also with strings or any other
value of the language, except nil.

Tables are the main (in fact, the only) data structuring mechanism in Lua,
 We use tables to represent ordinary arrays, sets, records,
and other data structures in a simple, uniform, and efficient way. Lua uses
tables to represent packages and objects as well.

When we write io.read, we think about “the read function from the io module”.

For Lua, this expression means “index the table io using the string read as the key”.

Tables in Lua are neither values nor variables; they are objects.    you do not have to declare
a table in Lua; in fact, there is no way to declare one. You create tables by means
of a constructor expression, which in its simplest form is written as {}:


a = {} -- create a table and store its reference in 'a'
k = "x"
a[k] = 10 -- new entry, with key="x" and value=10
a[20] = "great" -- new entry, with key=20 and value="great"
print(a["x"]) --> 10
k = 20
print(a[k]) --> "great"
a["x"] = a["x"] + 1 -- increments entry "x"
print(a["x"]) --> 11

a = {}
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 -- no references left to the table
When a program has no more references to a table, Lua’s garbage collector will
eventually delete the table and reuse its memory.
Each table can store values with different types of indices, and it grows as
needed to accommodate new entries:
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
Note the last line: like global variables, table fields evaluate to nil when not
initialized. Also like global variables, you can assign nil to a table field to delete
it. This is not a coincidence: Lua stores global variables in ordinary tables. We
will discuss this subject further in Chapter 14.
To represent records, you use the field name as an index. Lua supports this
representation by providing a.name as syntactic sugar for a["name"]. Therefore,
we could write the last lines of the previous example in a cleaner manner as
follows:
a.x = 10 -- same as a["x"] = 10
print(a.x) -- same as print(a["x"])
print(a.y) -- same as print(a["y"])


a = {}
for i = 1, 10 do
a[i] = io.read()
end


However, it is customary in Lua to
start arrays with one (and not with zero, as in C) and several facilities in Lua
stick to this convention


-- print the lines
for i = 1, #a do
print(a[i])
end


i = 10; j = "10"; k = "+10"
a = {}
a[i] = "one value"
a[j] = "another value"
a[k] = "yet another value"
print(a[i]) --> one value
print(a[j]) --> another value
print(a[k]) --> yet another value
print(a[tonumber(j)]) --> one value
print(a[tonumber(k)]) --> one value

You can introduce subtle bugs in your program if you do not pay attention to this
point.


2.6 Functions

Functions are first-class values in Lua: programs can store functions in variables,
pass functions as arguments to other functions, and return functions as
results


Lua can call functions written in Lua and functions written in C. Typically,
we resort to C functions both to achieve better performance and to access facilities
not easily accessible directly from Lua, such as operating-system facilities.
All the standard libraries in Lua are written in C. They comprise functions for
string manipulation, table manipulation, I/O, access to basic operating system
facilities, mathematical functions, and debugging.



2.7 Userdata and Threads
The userdata type allows arbitrary C data to be stored in Lua variables. It has
no predefined operations in Lua, except assignment and equality test. Userdata
are used to represent new types created by an application program or a library
written in C; for instance, the standard I/O library uses them to represent open
files. We will discuss more about userdata later, when we get to the C API.
We will explain the thread type in Chapter 9, where we discuss coroutines.


Exercise 2.1: What is the value of the expression type(nil)==nil? (You can
use Lua to check your answer.) Can you explain this result?

answer:false

nil 是一类型关键字,但type(nil) 得到的是nil 字符串 that's a="nil",in Lua,Conditional tests (e.g., conditions in
control structures) consider both the boolean false and nil as false and anything
else as true.


Exercise 2.2: Which of the following are valid numerals? What are their
values?
.0e12 .e12 0.0e 0x12 0xABFG 0xA FFFF 0xFFFFFFFF
0x 0x1P10 0.1e1 0x0.1p1


Exercise 2.2: Which of the following are valid numerals? What are their
values?
.0e12 .e12 (Error)   0.0e (Error,E的多少次幂)   0x12   0xABFG (Error, 哪有G) 0xA 

FFFF (Error, need prefix 0x)  0xFFFFFFFF
0x (Error)  0x1P10  0.1e1 0x0.1p1


Exercise 2.3: The number 12.7 is equal to the fraction 127/10, where the
denominator is a power of ten (也就是 127E-1,注意E是10不是自然对数 ).

Can you express it as a common fraction where  the denominator is a power of two?

a=0x10p3-1;    (16*2^3  -1 =127, or 0xFp3+7)  , Note:16 进制 p才能用

What about the number 5.5?


Exercise 2.4: How can you embed the following piece of XML as a string in
Lua?
<![CDATA[
Hello world
]]>

Show at least two different ways.

a=[=[
<![CDATA[
Hello world
]]>
]=];


b=把这些字符转成ASNCII. \\ddd 形式


Exercise 2.6: Assume the following code:
a = {}; a.a = a
What would be the value of a.a.a.a?    a.a  same as a["a"]. so a.a.a.a=a.  (print a is a table:table: 0063A3C0)


Is any a in that sequence somehow   different from the others? (Yes., first a , is a talbe, last 3 a is a key string)
Now, add the next line to the previous code:
a.a.a.a = 3
What would be the value of a.a.a.a now? {will hit error:attempt to index field 'a' (a number value) ,}




Table 的一综合例子

a={
"This is what",
strong="ni ta ma de la",
Add=function(a,b)
return a+b;
end,
otherTable={
trevor="trevor yang",
roase="Roserline"
}

};
a[2]="ni ta ma de";
a["my"]="I am trevor yang";
a["long"]=[=[
<![CDATA[
Hello world
]]>
]=];
kb=10;
print(a.strong);
print(a["strong"]);
print(a[1]);
print(a.Add(1,2));
print(a.otherTable.trevor);
 








 

































 














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值