🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 自定义方法 计算长度 ``` <pre class="calibre16">``` <span class="token5">function</span> <span class="token1">table_leng</span><span class="token2">(</span>t<span class="token2">)</span> local leng<span class="token">=</span><span class="token3">0</span> <span class="token5">for</span> k<span class="token2">,</span> v <span class="token5">in</span> <span class="token1">pairs</span><span class="token2">(</span>t<span class="token2">)</span> <span class="token5">do</span> leng<span class="token">=</span>leng<span class="token">+</span><span class="token3">1</span> end <span class="token5">return</span> leng<span class="token2">;</span> end <span class="token5">function</span> <span class="token1">trim</span><span class="token2">(</span>s<span class="token2">)</span> <span class="token5">return</span> <span class="token2">(</span>string<span class="token2">.</span><span class="token1">gsub</span><span class="token2">(</span>s<span class="token2">,</span> <span class="token4">"^%s*(.-)%s*$"</span><span class="token2">,</span> <span class="token4">"%1"</span><span class="token2">)</span><span class="token2">)</span> end 打印表 <span class="token5">function</span> print_r <span class="token2">(</span> t <span class="token2">)</span> local print_r_cache<span class="token">=</span><span class="token2">{</span><span class="token2">}</span> local <span class="token5">function</span> <span class="token1">sub_print_r</span><span class="token2">(</span>t<span class="token2">,</span>indent<span class="token2">)</span> <span class="token5">if</span> <span class="token2">(</span>print_r_cache<span class="token2">[</span><span class="token1">tostring</span><span class="token2">(</span>t<span class="token2">)</span><span class="token2">]</span><span class="token2">)</span> then <span class="token1">print</span><span class="token2">(</span>indent<span class="token2">.</span><span class="token2">.</span><span class="token4">"*"</span><span class="token2">.</span><span class="token2">.</span><span class="token1">tostring</span><span class="token2">(</span>t<span class="token2">)</span><span class="token2">)</span> <span class="token5">else</span> print_r_cache<span class="token2">[</span><span class="token1">tostring</span><span class="token2">(</span>t<span class="token2">)</span><span class="token2">]</span><span class="token">=</span><span class="token3">true</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">type</span><span class="token2">(</span>t<span class="token2">)</span><span class="token">==</span><span class="token4">"table"</span><span class="token2">)</span> then <span class="token5">for</span> pos<span class="token2">,</span>val <span class="token5">in</span> <span class="token1">pairs</span><span class="token2">(</span>t<span class="token2">)</span> <span class="token5">do</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">type</span><span class="token2">(</span>val<span class="token2">)</span><span class="token">==</span><span class="token4">"table"</span><span class="token2">)</span> then <span class="token1">print</span><span class="token2">(</span>indent<span class="token2">.</span><span class="token2">.</span><span class="token4">"["</span><span class="token2">.</span><span class="token2">.</span>pos<span class="token2">.</span><span class="token2">.</span><span class="token4">"] => "</span><span class="token2">.</span><span class="token2">.</span><span class="token1">tostring</span><span class="token2">(</span>t<span class="token2">)</span><span class="token2">.</span><span class="token2">.</span><span class="token4">" {"</span><span class="token2">)</span> <span class="token1">sub_print_r</span><span class="token2">(</span>val<span class="token2">,</span>indent<span class="token2">.</span><span class="token2">.</span>string<span class="token2">.</span><span class="token1">rep</span><span class="token2">(</span><span class="token4">" "</span><span class="token2">,</span>string<span class="token2">.</span><span class="token1">len</span><span class="token2">(</span>pos<span class="token2">)</span><span class="token">+</span><span class="token3">8</span><span class="token2">)</span><span class="token2">)</span> <span class="token1">print</span><span class="token2">(</span>indent<span class="token2">.</span><span class="token2">.</span>string<span class="token2">.</span><span class="token1">rep</span><span class="token2">(</span><span class="token4">" "</span><span class="token2">,</span>string<span class="token2">.</span><span class="token1">len</span><span class="token2">(</span>pos<span class="token2">)</span><span class="token">+</span><span class="token3">6</span><span class="token2">)</span><span class="token2">.</span><span class="token2">.</span><span class="token4">"}"</span><span class="token2">)</span> elseif <span class="token2">(</span><span class="token1">type</span><span class="token2">(</span>val<span class="token2">)</span><span class="token">==</span><span class="token4">"string"</span><span class="token2">)</span> then <span class="token1">print</span><span class="token2">(</span>indent<span class="token2">.</span><span class="token2">.</span><span class="token4">"["</span><span class="token2">.</span><span class="token2">.</span>pos<span class="token2">.</span><span class="token2">.</span><span class="token4">'] => "'</span><span class="token2">.</span><span class="token2">.</span>val<span class="token2">.</span><span class="token2">.</span><span class="token4">'"'</span><span class="token2">)</span> <span class="token5">else</span> <span class="token1">print</span><span class="token2">(</span>indent<span class="token2">.</span><span class="token2">.</span><span class="token4">"["</span><span class="token2">.</span><span class="token2">.</span>pos<span class="token2">.</span><span class="token2">.</span><span class="token4">"] => "</span><span class="token2">.</span><span class="token2">.</span><span class="token1">tostring</span><span class="token2">(</span>val<span class="token2">)</span><span class="token2">)</span> end end <span class="token5">else</span> <span class="token1">print</span><span class="token2">(</span>indent<span class="token2">.</span><span class="token2">.</span><span class="token1">tostring</span><span class="token2">(</span>t<span class="token2">)</span><span class="token2">)</span> end end end <span class="token5">if</span> <span class="token2">(</span><span class="token1">type</span><span class="token2">(</span>t<span class="token2">)</span><span class="token">==</span><span class="token4">"table"</span><span class="token2">)</span> then <span class="token1">print</span><span class="token2">(</span><span class="token1">tostring</span><span class="token2">(</span>t<span class="token2">)</span><span class="token2">.</span><span class="token2">.</span><span class="token4">" {"</span><span class="token2">)</span> <span class="token1">sub_print_r</span><span class="token2">(</span>t<span class="token2">,</span><span class="token4">" "</span><span class="token2">)</span> <span class="token1">print</span><span class="token2">(</span><span class="token4">"}"</span><span class="token2">)</span> <span class="token5">else</span> <span class="token1">sub_print_r</span><span class="token2">(</span>t<span class="token2">,</span><span class="token4">" "</span><span class="token2">)</span> end <span class="token1">print</span><span class="token2">(</span><span class="token2">)</span> end 去重复 <span class="token5">function</span> <span class="token1">table_unique</span><span class="token2">(</span>t<span class="token2">)</span> local check <span class="token">=</span> <span class="token2">{</span><span class="token2">}</span><span class="token2">;</span> local n <span class="token">=</span> <span class="token2">{</span><span class="token2">}</span><span class="token2">;</span> <span class="token5">for</span> key <span class="token2">,</span> value <span class="token5">in</span> <span class="token1">pairs</span><span class="token2">(</span>t<span class="token2">)</span> <span class="token5">do</span> <span class="token5">if</span> not check<span class="token2">[</span>value<span class="token2">]</span> then n<span class="token2">[</span>key<span class="token2">]</span> <span class="token">=</span> value check<span class="token2">[</span>value<span class="token2">]</span> <span class="token">=</span> value end end <span class="token5">return</span> n end ``` ```