Lua 中的字符串连接 [英] Concatenation of strings in Lua

查看:29
本文介绍了Lua 中的字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多语言中,您可以在变量赋值时连接字符串.我有一个场景,使用 Lua 编程语言,我需要将命令的输出附加到现有变量.Lua 中是否有与以下示例等效的功能?

In many languages you can concatenate strings on variable assignment. I have a scenario, using the Lua programming language, where I need to append the output of a command to an existing variable. Is there a functional equivalent in Lua to the below examples?

其他语言的示例:

===== PERL =====
$filename = "checkbook";
$filename .= ".tmp";
================

===== C# =====
string filename = "checkbook";
filename += ".tmp";
===============

预先感谢您的帮助.

推荐答案

正如其他答案所说,Lua 中的字符串连接运算符是两个点.

As other answers have said, the string concatenation operator in Lua is two dots.

你的简单例子会写成这样:

Your simple example would be written like this:

filename = "checkbook"
filename = filename .. ".tmp"

但是,有一点需要注意.由于 Lua 中的字符串是不可变的,每次连接都会创建一个新的字符串对象并将数据从源字符串复制到它.这使得连续连接到单个字符串的性能非常差.

However, there is a caveat to be aware of. Since strings in Lua are immutable, each concatenation creates a new string object and copies the data from the source strings to it. That makes successive concatenations to a single string have very poor performance.

这种情况下的 Lua 习语是这样的:

The Lua idiom for this case is something like this:

function listvalues(s)
    local t = { }
    for k,v in ipairs(s) do
        t[#t+1] = tostring(v)
    end
    return table.concat(t,"
")
end

通过收集数组t中要连接的字符串,可以使用标准库例程table.concat将它们全部连接起来(连同分隔符字符串每对之间)没有不必要的字符串复制.

By collecting the strings to be concatenated in an array t, the standard library routine table.concat can be used to concatenate them all up (along with a separator string between each pair) without unnecessary string copying.

更新: 我刚刚注意到我最初使用 pairs() 而不是 ipairs() 编写了上面的代码片段.

Update: I just noticed that I originally wrote the code snippet above using pairs() instead of ipairs().

正如最初编写的那样,函数 listvalues() 确实会从传入的表中生成每个值,但不是以稳定或可预测的顺序.另一方面,它将包括 1#s 范围内的键不是正整数的值.这就是 pairs() 所做的:它生成存储在表中的每个(键,值)对.

As originally written, the function listvalues() would indeed produce every value from the table passed in, but not in a stable or predictable order. On the other hand, it would include values who's keys were not positive integers in the span of 1 to #s. That is what pairs() does: it produces every single (key,value) pair stored in the table.

在大多数情况下,您将使用诸如 listvalueas() 之类的东西,您会对保留它们的顺序感兴趣.因此,编写为 listvalues{13, 42, 17, 4} 的调用将生成一个包含这些值的字符串.但是,pairs() 不会那样做,它会以某种顺序逐项列出它们,这取决于表数据结构的底层实现.众所周知,顺序不仅取决于密钥,还取决于插入密钥和取出其他密钥的顺序.

In most cases where you would be using something like listvaluas() you would be interested in preserving their order. So a call written as listvalues{13, 42, 17, 4} would produce a string containing those value in that order. However, pairs() won't do that, it will itemize them in some order that depends on the underlying implementation of the table data structure. It is known that the order not only depends on the keys, but also on the order in which the keys were inserted and other keys removed.

当然 ipairs() 也不是一个完美的答案.它只枚举表中形成序列"的那些值.也就是说,那些作为键的值形成了一个完整的块,从 1 到某个上限,这(通常)也是 # 运算符返回的值.(在许多情况下,函数 ipairs() 本身最好用更简单的 for 循环代替,该循环仅从 1#s.这是在 Lua 5.2 和 LuaJIT 中推荐的做法,其中更简单的 for 循环可以比 ipairs() 迭代器更有效地实现.)

Of course ipairs() isn't a perfect answer either. It only enumerates those values of the table that form a "sequence". That is, those values who's keys form an unbroken block spanning from 1 to some upper bound, which is (usually) also the value returned by the # operator. (In many cases, the function ipairs() itself is better replaced by a simpler for loop that just counts from 1 to #s. This is the recommended practice in Lua 5.2 and in LuaJIT where the simpler for loop can be more efficiently implemented than the ipairs() iterator.)

如果 pairs() 确实是正确的方法,那么通常情况下您希望同时打印键和值.这通过使数据具有自描述性来减少对顺序的担忧.当然,由于任何Lua类型(除了nil和浮点NaN)都可以作为key(NaN也可以存储作为一个值)找到一个字符串表示留给学生作为练习.并且不要忘记树和更复杂的表结构.

If pairs() really is the right approach, then it is usually the case that you want to print both the key and the value. This reduces the concerns about order by making the data self-describing. Of course, since any Lua type (except nil and the floating point NaN) can be used as a key (and NaN can also be stored as a value) finding a string representation is left as an exercise for the student. And don't forget about trees and more complex structures of tables.

这篇关于Lua 中的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆