我是个新手,所以我希望你能帮我一把。
我正在编程灯光,我喜欢做的是从我的照明桌面上提取一个变量(一个称为"4 Mythos Stage“的文本字符串),并将其拆分为不同的变量。
要从我使用的工作台中获取变量:
return function ()
local Layer1 = gma.user.getvar("Layer1") -- I placed "4 Mythos Stage" variable in Layer1
gma.feedback(Layer1) -- gives feedback 4 Mythos Stage
end现在,我想将字符串拆分为3个新的局部变量,名为:
local number -- should produce 4
local fixturetype -- should produce Mythos
local location -- should produce Stage我尝试了以下几种方法:
local number = string.match('Layer1', '%d+')
local fixturetype = string.match('Layer1', '%a+')
local location = string.match('Layer1', '%a+')这不管用,所以有没有人能帮我找到正确的方向。我会很高兴的。
向您致以亲切的问候,
马提金
发布于 2018-04-05 06:11:49
您可以同时赋值这三个变量,因为Lua具有多个返回和多个赋值。将每个模式用括号括起来,以便将它们作为捕获返回,并将它们组合成一个模式,其间有空格:
local number, fixturetype, location = string.match(Layer1, '(%d+) (%a+) (%a+)')如果要在项之间使用多个空格或制表符,则此模式会更好:
local number, fixturetype, location = string.match(Layer1, '(%d+)[ \t]+(%a+)[ \t]+(%a+)')您的尝试失败的原因是因为string.match('Layer1', '%d+')在'Layer1' (一个字符串)而不是Layer1 (一个变量)内进行搜索。
但是,即使纠正了这一点,每次调用string.match(Layer1, '%a+') (其中是Layer1 == '4 Mythos Stage')时也会得到'Mythos'。除非在第三个参数string.match(Layer1, '%a+', 9) --> 'Stage'中提供索引,否则string.match始终从字符串的开头开始。
发布于 2018-04-05 08:49:23
此任务的一个健壮的解决方案是将字符串拆分为三个“单词”,其中一个单词是一系列非空格字符:
local number, fixturetype, location = string.match(Layer1, '(%S+)%s+(%S+)%s+(%S+)')https://stackoverflow.com/questions/49660603
复制相似问题