企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Lua语言 - 安装 <http://joedf.ahkscript.org/LuaBuilds/> 注释--\[\[ 注释--\]\] 或单行 --开始,操作符号不等于 ~= 逻辑运算 and or not ;连接运算..;计算字符长度`#"你好"`;分号可有可无 - 数据类型8种 nil 无效值,条件中表示false boolean布尔型 false true两个值 number 双精度浮点数 string字符串,单引号双引号 userdata存储在变量中C数据类型 function函数由C或Lua编写 thread独立线程,用于协程 table 表类型,可以用来支持数组用{}建立空表 - 变量,默认值nil ``` <pre class="calibre14">``` b<span class="token">=</span><span class="token3">1</span> 全局变量 local b<span class="token">=</span> <span class="token3">5</span> 局部变量 a<span class="token2">,</span>b<span class="token">=</span><span class="token3">1</span><span class="token2">,</span><span class="token3">2</span> 同时赋值a<span class="token2">,</span>b也可用来交换a<span class="token2">,</span>b值比如<span class="token2">:</span>a<span class="token2">,</span>b<span class="token">=</span>b<span class="token2">,</span>a tab<span class="token">=</span><span class="token2">{</span>a<span class="token">=</span><span class="token3">1</span><span class="token2">,</span>b<span class="token">=</span><span class="token4">"wo我"</span><span class="token2">}</span> tab<span class="token2">[</span><span class="token4">"a"</span><span class="token2">]</span> 或tab<span class="token2">.</span>a tab<span class="token">=</span><span class="token2">{</span><span class="token3">1</span><span class="token2">,</span><span class="token4">"wo我"</span><span class="token2">}</span> tab<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span>是<span class="token3">1</span>索引从<span class="token3">1</span>开始 没有索引只能tab<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span>一种调用方法 ``` ``` - 无限循环,break跳出当前循环语句 ``` <pre class="calibre14">``` <span class="token5">while</span><span class="token2">(</span> <span class="token3">true</span> <span class="token2">)</span> <span class="token5">do</span> <span class="token1">print</span><span class="token2">(</span><span class="token4">"循环将永远执行下去"</span><span class="token2">)</span> end <span class="token5">for</span> i<span class="token">=</span><span class="token3">1</span><span class="token2">,</span><span class="token3">10</span><span class="token2">,</span><span class="token3">1</span> <span class="token5">do</span> 输出<span class="token3">1</span>到<span class="token3">10</span> <span class="token1">print</span><span class="token2">(</span>i<span class="token2">)</span> end <span class="token5">for</span> i<span class="token">=</span><span class="token3">10</span><span class="token2">,</span><span class="token3">1</span><span class="token2">,</span><span class="token">-</span><span class="token3">1</span> <span class="token5">do</span> 输出<span class="token3">10</span><span class="token">-</span><span class="token3">1</span> <span class="token1">print</span><span class="token2">(</span>i<span class="token2">)</span> end a <span class="token">=</span> <span class="token2">{</span><span class="token4">"one"</span><span class="token2">,</span> <span class="token4">"two"</span><span class="token2">,</span> <span class="token4">"three"</span><span class="token2">}</span> <span class="token5">for</span> i<span class="token2">,</span> v <span class="token5">in</span> <span class="token1">ipairs</span><span class="token2">(</span>a<span class="token2">)</span> <span class="token5">do</span> <span class="token1">print</span><span class="token2">(</span>i<span class="token2">,</span> v<span class="token2">)</span> end a<span class="token">=</span><span class="token3">1</span> 先输出<span class="token3">1</span><span class="token">-</span><span class="token3">15</span> repeat <span class="token1">print</span><span class="token2">(</span><span class="token4">"a的值为:"</span><span class="token2">,</span> a<span class="token2">.</span><span class="token2">.</span><span class="token4">"<br>"</span><span class="token2">)</span> a <span class="token">=</span> a <span class="token">+</span> <span class="token3">1</span> <span class="token1">until</span><span class="token2">(</span> a <span class="token">></span> <span class="token3">15</span> <span class="token2">)</span> ``` ``` - 条件语句 nil false才为假,0是真,没有switch语句 ``` <pre class="calibre14">``` a<span class="token">=</span><span class="token3">1</span> <span class="token5">if</span><span class="token2">(</span>a<span class="token">></span><span class="token3">0</span><span class="token2">)</span> then <span class="token1">print</span><span class="token2">(</span><span class="token4">"大于1"</span><span class="token2">)</span> <span class="token1">elseif</span><span class="token2">(</span>a<span class="token">==</span><span class="token3">0</span><span class="token2">)</span> then <span class="token5">else</span> <span class="token1">print</span><span class="token2">(</span><span class="token4">"小于0"</span><span class="token2">)</span> end ``` ``` - 函数,支持多个返回值,参数可变,..是链接符号,#arg计算长度 ``` <pre class="calibre16">``` arg <span class="token">=</span> <span class="token1">select</span><span class="token2">(</span><span class="token4">"#"</span><span class="token2">,</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span><span class="token2">)</span><span class="token2">;</span> 可变参数数量 <span class="token5">function</span> <span class="token1">average</span><span class="token2">(</span><span class="token2">.</span><span class="token2">.</span><span class="token2">.</span><span class="token2">)</span> result <span class="token">=</span> <span class="token3">0</span> local arg<span class="token">=</span><span class="token2">{</span><span class="token2">.</span><span class="token2">.</span><span class="token2">.</span><span class="token2">}</span> <span class="token">--</span><span class="token">></span> arg 为一个表,局部变量 <span class="token5">for</span> i<span class="token2">,</span>v <span class="token5">in</span> <span class="token1">ipairs</span><span class="token2">(</span>arg<span class="token2">)</span> <span class="token5">do</span> result <span class="token">=</span> result <span class="token">+</span> v end <span class="token1">print</span><span class="token2">(</span><span class="token4">"总共传入 "</span> <span class="token2">.</span><span class="token2">.</span> #arg <span class="token2">.</span><span class="token2">.</span> <span class="token4">" 个数"</span><span class="token2">)</span> <span class="token5">return</span> result<span class="token">/</span>#arg end ``` ```