当前位置:网站首页>lua语言的左对齐函数(手写)
lua语言的左对齐函数(手写)
2022-07-28 17:39:00 【h799710】
一、前言:
lua 的制表符还是不够强大,当某一行特别大的时候,后面就全乱套了。这语言又不像python一样有这丰富的库可以调用。没办法,手写一个吧。
二、代码如下:
逻辑很简单,就是先遍历一遍数据,记录每一列的最大长度。
然后根据 当前列最大长度 - 具体的数据长度 获得需要填充空格即可;
-- 将二维数组以左对齐的形式拼接成字符串并返回
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
-- 填充空格
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)
三、结尾
逻辑很简单,就是写的时候费了点时间。需要的直接拿来用就可以了
这个方法如果是对齐中文还是会有些偏差,代码里面的注释是稍微修正偏差的方法,取消注释就可以用。
边栏推荐
- 剑指 Offer II 109. 开密码锁
- Rust 入门指南(crate 管理)
- Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
- 这种动态规划你见过吗——状态机动态规划之股票问题(下)
- Pytoch: implementation of crossentropyloss and labelsmoothing
- SaltStack常用的模块
- Jestson nano Object detection
- An intern's journey to cnosdb
- 当CNN遇见Transformer《CMT:Convolutional Neural Networks Meet Vision Transformers》
- How to use Qianqian listening sound effect plug-in (fierce Classic)
猜你喜欢

Gmoea code operation 2 -- establishment and operation of operation environment

From Bayesian filter to Kalman filter (2)

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

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation

用于异常检测的Transformer - InTra《Inpainting Transformer for Anomaly Detection》

Self-adaptive multi-objective evolutionary algorithm for flexible job shop scheduling with fuzzy pro

NDK 系列(5):JNI 从入门到实践,爆肝万字详解!

Application value of MES production management system to equipment

Solve the critical path in FJSP - with Matlab source code

R language text mining and natural language processing tutorial
随机推荐
身份证号的奥秘
英语文章翻译-英语文章翻译软件-免费批量翻译
Photoshop web design practical tutorial
RFs self study notes (II): theoretical measurement model - without clutter but with detection probability
微信公众号授权登录后报redirect_uri参数错误的问题
Mid 2022 summary
宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0
Pytorch:交叉熵损失(CrossEntropyLoss)以及标签平滑(LabelSmoothing)的实现
Dockler的基础用法
SaltStack之salt-ssh
Ardupilot software in the loop simulation and online debugging
Libgdx learning road 02: draw game map with tiled
ES6 conversion of new data type set and arr set map
2022年中总结
以数字化转型为契机,3C企业如何通过SRM供应商云协同平台实现高效协同?
Validate hardware DDR design with Xilinx MIG
使用Xilinx MIG验证硬件DDR设计
Smart contract security - overflow vulnerability
Application of time series database in museum environment detection
Srs4.0 installation steps