学习Lua--1

1、标识符

Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit.

 Identifiers are used to name variables and table fields.

The following keywords are reserved and cannot be used as names:

     and       break     do        else      elseif
     end       false     for       function  if
     in        local     nil       not       or
     repeat    return    then      true      until     while

 v5.4 增加    goto

 

1-1、名称可以是任意 字母,数字,下划线 组成,但是能以数字开头  Lua语言的关键字 21 个 

and break do else elseif end false true for function if in local nil not or repeat return then until while

 

Lua is a case-sensitive language: and is a reserved word, but And and AND are two different, valid names. As a convention, 

names starting with an underscore followed by uppercase letters (such as _VERSION) are reserved for internal global variables used by Lua.

1-2、Lua 是大小写敏感的(case-sensitive),注意:以下划线开始后面全部大写的字母组成的字符串 是系统保留的内部全局变量,用户不可使用

 



The following strings denote other tokens:

     +     -     *     /     %     ^     #
     ==    ~=    <=    >=    <     >     =
     (     )     {     }     [     ]
     ;     :     ,     .     ..    ...
ver 5.4 增加
::
Literal strings can be delimited by matching single or double quotes, and can contain the following C-like escape 
sequences: '\a' (bell), '\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab),
'\v' (vertical tab), '\\' (backslash), '\"' (quotation mark [double quote]), and '\'' (apostrophe [single quote]).
Moreover, a backslash followed by a real newline results in a newline in the string.
A character in a string can also be specified by its numerical value using the escape sequence \ddd,
where ddd is a sequence of up to three decimal digits.
(Note that if a numerical escape is to be followed by a digit, it must be expressed using exactly three digits.)
Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as
'\0'.

 

2、有关字符串

2-1:以下字符 串有特殊意义:

     +     -     *     /     %     ^     #
     ==    ~=    <=    >=    <     >     =
     (     )     {     }     [     ]
     ;     :     ,     .     ..    ...

2-2:转义字符

在Lua 中字符串中可以包含 类似C 语言中转义字符:

下面对C语言中的转义字符进行补充

字符集(Character Set)为每个字符分配了唯一的编号,我们不妨将它称为编码值。在C语言中,一个字符除了可以用它的实体(也就是真正的字符)表示,还可以用编码值表示。
这种使用编码值来间接地表示字符的方式称为转义字符(Escape Character)。
转义字符的初衷是用于 ASCII 编码,所以它的取值范围有限:
八进制形式的转义字符最多后跟三个数字,也即\ddd,最大取值是\177;
十六进制形式的转义字符最多后跟两个数字,也即\xdd,最大取值是\x7f。

超出范围的转义字符的行为是未定义的,有的编译器会将编码值直接输出,有的编译器会报错。

对于 ASCII 编码,0~31(十进制)范围内的字符为控制字符,它们都是看不见的,不能在显示器上显示,甚至无法从键盘输入,只能用转义字符的形式来表示。不过,直接使用 ASCII 码记忆不方便,也不容易理解,
所以,针对常用的控制字符,C语言又定义了简写方式,完整的列表如下:
转义字符    意义    ASCII码值(十进制)
\a    响铃(BEL)    007
\b    退格(BS) ,将当前位置移到前一列    008
\f    换页(FF),将当前位置移到下页开头    012
\n    换行(LF) ,将当前位置移到下一行开头    010
\r    回车(CR) ,将当前位置移到本行开头    013
\t    水平制表(HT)     009
\v    垂直制表(VT)    011
\'    单引号    039
\"    双引号    034
\\    反斜杠    092

\n和\t是最常用的两个转义字符:
\n用来换行,让文本从下一行的开头输出,前面的章节中已经多次使用;
\t用来占位,一般相当于四个空格,或者 tab 键的功能。

单引号、双引号、反斜杠是特殊的字符,不能直接表示:
单引号是字符类型的开头和结尾,要使用\'表示,也即'\'';
双引号是字符串的开头和结尾,要使用\"表示,也即"abc\"123";
反斜杠是转义字符的开头,要使用\\表示,也即'\\',或者"abc\\123"

 

在Lua 中转义字符与C中的用法一样,需要注意的是 当一个字符用它对应的值与反斜杠配合表示时(\ddd) 
1、ddd 位数必须 是3 位 不足3 位前面补0
2、ddd的值的进制 为 十进制 不同的字符的ASCII值 (十进制)对照表如下:

数字 32–126 分配给了能在键盘上找到的字符,当您查看或打印文档时就会出现。注:十进制32代表空格 ,十进制数字 127 代表 DELETE 命令。下面是ASCII码和相应数字的对照表

ASCII 码 字符   ASCII 码 字符   ASCII 码 字符   ASCII 码 字符
十进位 十六进位   十进位 十六进位   十进位 十六进位   十进位 十六进位
032 20     056 38 8   080 50 P   104 68 h
033 21 !   057 39 9   081 51 Q   105 69 i
034 22 "   058 3A :   082 52 R   106 6A j
035 23 #   059 3B ;   083 53 S   107 6B k
036 24 $   060 3C <   084 54 T   108 6C l
037 25 %   061 3D =   085 55 U   109 6D m
038 26 &   062 3E >   086 56 V   110 6E n
039 27 '   063 3F ?   087 57 W   111 6F o
040 28 (   064 40 @   088 58 X   112 70 p
041 29 )   065 41 A   089 59 Y   113 71 q
042 2A *   066 42 B   090 5A Z   114 72 r
043 2B +   067 43 C   091 5B [   115 73 s
044 2C ,   068 44 D   092 5C \   116 74 t
045 2D -   069 45 E   093 5D ]   117 75 u
046 2E .   070 46 F   094 5E ^   118 76 v
047 2F /   071 47 G   095 5F _   119 77 w
048 30 0   072 48 H   096 60 `   120 78 x
049 31 1   073 49 I   097 61 a   121 79 y
050 32 2   074 4A J   098 62 b   122 7A z
051 33 3   075 4B K   099 63 c   123 7B {
052 34 4   076 4C L   100 64 d   124 7C |
053 35 5   077 4D M   101 65 e   125 7D }
054 36 6   078 4E N   102 66 f   126 7E ~
055 37 7   079 4F O   103 67 g   127 7F DEL

 

  转义字符使用例子:

     a = 'alo\n123"'
     a = "alo\n123\""
     a = '\97lo\10\04923"'

 

 

2-3:字符串拼接

在Lua中,长的字符串,如果写在一行中 可以在开始和结束的位置用 signal or double quotes 单引号或双引号包裹,如果 想多行书写可以在行结束 位置 以 反斜杠 “/” 结束,

然后在下一行继续该字符串。

如:

eval "if ( redis.call ('get',KEYS[1]) == ARGV[1] ) then \
    return redis.call ( 'del',KEYS[1] ) \
else \
    return 0 \
end" 1 lock:order:1 thread-33

 

Literal strings can also be defined using a long format enclosed by long brackets. We define an opening long bracket of level n as an opening square bracket followed by n equal signs followed by another opening square bracket. So, an opening long bracket of level 0 is written as [[, an opening long bracket of level 1 is written as [=[, and so on. A closing long bracket is defined similarly; for instance, a closing long bracket of level 4 is written as ]====]. A long string starts with an opening long bracket of any level and ends at the first closing long bracket of the same level. Literals in this bracketed form can run for several lines, do not interpret any escape sequences, and ignore long brackets of any other level. They can contain anything except a closing bracket of the proper level.

2.4:字符串长格式(理解为不带转义字符的多行拼接)

长格式就是 下面的结构:

A long string starts with an opening long bracket of any level and ends at the first closing long bracket of the same level.

开始标志:两个开始中括号 中间包含任意个数的等号  A long string starts with an opening long bracket of any level 

字符串内容:任意字符,且转义字符无效(当成普字符对待)

结束标志:两个结束中括号中间包含任意个数的等号(等号个数必须等于开始标志中的等号个数 ) 如果有多个结束标志以第一个为准这就意味着,后面的内容将是无效的。 ends at the first closing long bracket of the same level.

 

用正则表达式就是:^[=*[^(?!.*(=*]))]=*]$  ( 未验证 如果不正确,请在留言留言,谢谢。)

 

 

 

错误的结束匹配

 

 

3、数值

数值类型的表示 整数,小数,科学计数法 0x16进制

numerical constant can be written with an optional decimal part and an optional decimal exponent. Lua also accepts integer hexadecimal constants, by prefixing them with 0x. Examples of valid numerical constants are

 3   3.0   3.1416   314.16e-2   0.31416E1   0xff   0x56

4、注释

comment starts with a double hyphen (--) anywhere outside a string. If the text immediately after -- is not an opening long bracket, the comment is a short comment, which runs until the end of the line. Otherwise, it is a long comment, which runs until the corresponding closing long bracket. Long comments are frequently used to disable code temporarily.

4.1: 短注释

-- 后面直接跟注释内容 直到这行结束

4.2:长注释  长格式的开始结束同上字符串规则

--[=*[

被注释的多行内容

--]=*]

 

 

未完待续。。。。

 

 

 

 

 

 

 

 

    

 

posted on 2023-03-04 15:49  hztech  阅读(24)  评论(0编辑  收藏  举报

导航