当前位置:网站首页>Left alignment function of Lua language (handwriting)
Left alignment function of Lua language (handwriting)
2022-07-28 19:36:00 【h799710】
One 、 Preface :
lua The tabs of are still not strong enough , When a line is very big , It's all messed up behind . This language is not like python There are also rich libraries that can be called . Can't , Write one by hand .
Two 、 The code is as follows :
The logic is simple , Is to traverse the data first , Record the maximum length of each column .
And then according to The maximum length of the front row - Specific data length Get the blank space you need to fill ;
-- Concatenate a two-dimensional array into a string in the form of left alignment and return
function leftAlignString(data)
local data2 = {
}
local colLenList ={
}
local len, vlen = #data[1]
for i, v in ipairs(data) do
data2[i] = {
}
for vi, vd in ipairs(v) do
if type(vd) ~= "string" then
vd = tostring(vd)
end
data2[i][#data2[i] + 1] = vd
if vi ~= len then
data2[i][#data2[i] + 1] = ""
-- if i == 1then
-- colLenList[vi] = math.max(colLenList[vi] or 0, #vd / 3 * 2 + 4)
-- else
-- if #vd % 2 == 1 then
-- vlen = #vd + 1
-- else
vlen = #vd
-- end
colLenList[vi] = math.max(colLenList[vi] or 0, vlen + 2)
-- end
else
data2[i][#data2[i] + 1] = "\n"
end
end
end
-- Fill in the blanks
function bEmpty(count)
local str = ""
local fourEmpty = " ";
while count > 0 do
if count >= 4 then
str = str .. fourEmpty
count = count - 4
else
str = str .. " "
count = count - 1
end
end
return str
end
local empty = 0
for i, v in ipairs(data2) do
for k = 1, (len - 1) * 2 - 1, 2 do
-- if i == 1 then
-- -- print(v[k], k, colLenList[math.ceil(k / 2)], #v[k] / 3 * 2)
-- data2[i][k + 1] = bEmpty(colLenList[math.ceil(k / 2)] - #v[k] / 3 * 2)
-- else
-- print(v[k], k, colLenList[math.ceil(k / 2)], #v[k])
data2[i][k + 1] = bEmpty(colLenList[math.ceil(k / 2)] - #v[k])
-- end
end
data2[i] = table.concat(v)
end
return table.concat(data2)
end
local data = {
{
"index", "itemID", "randValue", "changeWeight", "initWeight", "isCompenate", "compenateData"},
{
1,52301,357229,1000000,1000000,false,"{}"},
{
1,52301,357229,1000000,1044400000,false,"{}"},
{
1,52301,357229,100000440,1000000,false,"{}"},
{
1,52301,357444229,1000000,1000000,false,"{}"},
{
2,51302,502332,26000,1875, false, "{}"},
}
data2 = leftAlignString(data)
print(data2)
3、 ... and 、 ending
The logic is simple , It took a little time to write . Just use what you need
If this method is aligned with Chinese, there will still be some deviation , The comments in the code are Slightly correct the deviation Methods , If you cancel the comment, you can use .
边栏推荐
- [notes] Networking: Internet product managers change the world
- Design of library management database system
- 初步学习函数(第3篇博客)
- New this prototype precompiled exercise
- Getting started with saltstack
- Smart contract security - overflow vulnerability
- Rust Getting Started Guide (rustup, cargo)
- redis 主从架构(sizeof函数怎么计算)
- Method of win7 system anti ARP attack
- English article translation - English article translation software - free batch translation
猜你喜欢

Business visualization - let your flowchart "run" (4. Actual business scenario test)

文章翻译软件-批量免费翻译软件支持各大翻译接口
![[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements](/img/1c/6dbfcb5e6ade52d8cbfabcb34616a5.png)
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements

NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!

Adobe XD web design tutorial

Rust Getting Started Guide (modules and engineering structures)

OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)

App自动化测试是怎么实现H5测试的

企业级分布式爬虫框架入门

When CNN meets transformer cmt:revolutionary neural networks meet vision transformers
随机推荐
JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover
OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
Business visualization - let your flowchart "run" (4. Actual business scenario test)
芯片功耗性能验证:从困境到超越
Design of library management database system
CVPR21-无监督异常检测《CutPaste:Self-Supervised Learning for Anomaly Detection and Localization》
Jestson nano Object detection
stc12c5a60s2功能说明(STC12C5A60S2默认触发)
First blog
shared_ptr 和 make_shared 的使用
Avoidance Adjusted Climbrate
Rust 入门指南(modules 和工程结构)
The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
Amazon launched Amazon one palm payment system, and the contactless palm vein recognition market is expected to explode
Preliminary learning function (3rd blog)
[深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
Sword finger offer II 109. unlock the password lock
Verification of chip power consumption performance: from dilemma to transcendence
初步学习函数(第3篇博客)
Pandownload revival tutorial