本文介绍了Lua Control Statments有奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从基本的控制语句操作中得到了一个非常意外的结果。我有以下内容,正在使用此类数据读取文件:

I'm getting a very unexpected result from what should be basic control statement operations. I have the following, a file being read with this sort of data:

1, 51, one , ab
1, 74, two , ab
0, 74, tree , ab
0, 74, for , ab
0, 74, five , ab

我处理它的Lua代码:

My snip of Lua code that processes it:

if file then
for line in file:lines() do
LineArray = line
CanClaimInfo[LineArray] = {}
lineData = utils.split(line,",")
    if lineData[1] == "0"  then
        lineData[1] = "CAN A"
    elseif lineData[1] == "1" then
        lineData[1] = "CAN B"
    else
        lineData[1] = lineData[1]
    end
CanClaimInfo[LineArray]["CANBus"] = lineData[1]
CanClaimInfo[LineArray]["Address"] = lineData[2]
CanClaimInfo[LineArray]["Name"] = lineData[3]
end

我将此作为输出:

CAN A   74  for
CAN A   74  tree
CAN A   74  five
CAN B   74  two
1       51  one

我不知道它是如何滑过 elseif lineData [1 ] ==1然后位。我查了一下,没有铅/尾随空格或类似的东西。有什么想法吗?

I don't get how it slips through the elseif lineData[1] == "1" then bit. I checked and there are no lead/trailing white spaces or anything like that. Any ideas?

推荐答案

在文件开头可能是utf-8编码字节?尝试在if测试之前打印lineData [1]以查看它是什么,并在 print(#lineData [1])中查看它有多少个字符。可能超过1个字符,所以它最终在第三个分支中(否则):

Maybe utf-8 encoding bytes at the beginning of file? Try printing lineData[1] before the "if" tests to see what it is, and print(#lineData[1]) to see how many chars it has. Likely more than 1 char so it ends up in that third branch (else):

lineData = utils.split(line,",")
print(#lineData[1]) -- likely prints 1 for all but first line
if lineData[1] == "0"  then

要查找额外字节,请尝试 print(string.byte(lineData [1],1,#lineData [1] ))

To find the extra bytes, try print(string.byte(lineData[1], 1, #lineData[1])).

这篇关于Lua Control Statments有奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:02