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

查看:88
本文介绍了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,"\n")
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.

在大多数情况下,如果您使用类似listvaluas()之类的东西,则可能会对保留其顺序感兴趣.因此,以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除外)用作键(并且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天全站免登陆