Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua:处理文本输出的所有排列并返回可选文本_Lua_Permutation - Fatal编程技术网

Lua:处理文本输出的所有排列并返回可选文本

Lua:处理文本输出的所有排列并返回可选文本,lua,permutation,Lua,Permutation,很久以前,我就在这个特定的Lua代码上寻求帮助,这对我很有帮助。现在我想对它做些改变,但我不确定我的做法是否正确 首先,在我玩的泥地上,我们有几个标志可以作为装备项目的前缀。它们是:(K),(M),(I),(G),和(H)。无论实际存在哪些标志,它们都将始终按该顺序出现。例如: (K)(M)(G)(H) (M)(H) (K)(I)(G) 等等。我使用的代码只是将上述组合分别更改为KMGH、MH、和KIG。我现在要做的是匹配任意字母组合并返回它们,用括号括起来,所以[KMGH],等等 for i

很久以前,我就在这个特定的Lua代码上寻求帮助,这对我很有帮助。现在我想对它做些改变,但我不确定我的做法是否正确

首先,在我玩的泥地上,我们有几个标志可以作为装备项目的前缀。它们是:
(K)
(M)
(I)
(G)
,和
(H)
。无论实际存在哪些标志,它们都将始终按该顺序出现。例如:

(K)(M)(G)(H)

(M)(H)

(K)(I)(G)

等等。我使用的代码只是将上述组合分别更改为
KMGH
MH
、和
KIG
。我现在要做的是匹配任意字母组合并返回它们,用括号括起来,所以
[KMGH]
,等等

for i = 1, #TriggerStyleRuns do

    TSRt = TriggerStyleRuns[i].text
    if string.match(TSRt,"(K)") or string.match(TSRt,"(I)") or string.match(TSRt,"(M)") or string.match(TSRt, "(G)") or string.match(TSRt, "(H)") then
        TriggerStyleRuns[i].text = string.gsub(TSRt,"%%((%%w)%%)","%%1")
    end
end
使用上面的代码,我能预见我的计划发生的唯一方式是展望每一个组合,但我真的不想有几行的计划

if string.match(TriggerStyleRuns[i].text, "(K)") and string.match(TriggerStyelRuns[i+1].text, "(M)" and...
因为虽然我能做到,但这需要很多额外的工作。是否有一种更简单的方法来处理它们,并确保找到的第一个元素在开始时收到
[
,而找到的最后一个元素收到
]

额外的好处:我想从中去掉
(I)
,并用它给括号上色,这要求我在第一个元素之前插入table.insert,在最后一个元素之后插入。有关添加的参考,请参见下面的TriggerStyleRuns样式表:

TriggerStyleRuns = {
  {"backcolour"=0,"text"="( 7) ", "length"=5, "style"=1, "textcolour"=16777215}, 
  {"backcolour"=0,"text"="(K)", "length"=3, "style"=1, "textcolour"=255},
  {"backcolour"=0, "text"="(M)", "length"=3, "style"=1, "textcolour"=16711680},
  {"backcolour"=0, "text"="(G)", "length"=3, "style"=1, "textcolour"=16777215},
  {"backcolour"=0, "text"="(H) ", "length"=4, "style"=1, "textcolour"=16776960},
  {"backcolour"=0, "text"="a ", "length"=2, "style"=0, "textcolour"=12632256},
  {"backcolour"=0, "text"="Bag of ", "length"=7, "style"=1, "textcolour"=65535},
  {"backcolour"=0, "text"="Aardwolf", "length"=8, "style"=1, "textcolour"=255},
  {"backcolour"=0, "text"=" ", "length"=1, "style"=0, "textcolour"=12632256},
  {"backcolour"=0, "text"="(", "length"=1, "style"=1, "textcolour"=16777215},
  {"backcolour"=0, "text"="72", "length"=2, "style"=1, "textcolour"=65280},
 {"backcolour"=0, "text"=")", "length"=1, "style"=1, "textcolour"=16777215}
}
索引的数量可以改变,但我最关心的是上面描述的标志

编辑
for
循环遍历
TriggerStyleRuns
表。上面给出了一个例子。如前所述,索引的数量可以更改,这包括标志的数量。例如,如果(K)标志不存在,在上表中,第二个索引将代替(M),而不是(K)。因此,本质上,我需要遍历该表,提取所有标志匹配项,更改这些索引中的文本,并在第一个标志之前和之后的位置创建一个新标志。例如,完成时,上表将如下所示:

TriggerStyleRuns = {
  {"backcolour"=0,"text"="( 7) ", "length"=5, "style"=1, "textcolour"=16777215}, 
  {"backcolour"=0,"text"="[", "length"=1, "style"=1, "textcolour"=1234567},
  {"backcolour"=0,"text"="K","length"="1", "style"=1, "textcolour"=255}, 
  {"backcolour"=0, "text"="M", "length"=3, "style"=1, "textcolour"=16711680},
  {"backcolour"=0, "text"="G", "length"=3, "style"=1, "textcolour"=16777215},
  {"backcolour"=0, "text"="H ", "length"=4, "style"=1, "textcolour"=16776960},
  {"backcolour"=0,"text"="]", "length"=1, "style"=1, "textcolour"=1234567},
  {"backcolour"=0, "text"="a ", "length"=2, "style"=0, "textcolour"=12632256},
  {"backcolour"=0, "text"="Bag of ", "length"=7, "style"=1, "textcolour"=65535},
  {"backcolour"=0, "text"="Aardwolf", "length"=8, "style"=1, "textcolour"=255},
  {"backcolour"=0, "text"=" ", "length"=1, "style"=0, "textcolour"=12632256},
  {"backcolour"=0, "text"="(", "length"=1, "style"=1, "textcolour"=16777215},
  {"backcolour"=0, "text"="72", "length"=2, "style"=1, "textcolour"=65280},
  {"backcolour"=0, "text"=")", "length"=1, "style"=1, "textcolour"=16777215}
}

我并没有真正理解你的描述,但据我所知,你想做的是,给定这些字母的任意顺序,按指定的顺序返回它们,并用方括号括起来。这是一种选择:

function orderOptions( str )
    local letters = { k = '', m = '', i = '', g = '', h = '' }
    str:gsub( '%((.)%)', function( letter )
        letters[letter] = letter
    end )
    return '[' .. letters.k .. letters.m .. letters.i .. letters.g .. letters.h .. ']'
end
但是,假设它们已经按正确的顺序排列,有什么问题呢

for i = 1, #TriggerStyleRuns do

    TSRt = TriggerStyleRuns[i].text
    if string.match(TSRt,"(K)") or string.match(TSRt,"(I)") or string.match(TSRt,"(M)") or string.match(TSRt, "(G)") or string.match(TSRt, "(H)") then
        TriggerStyleRuns[i].text = string.gsub(TSRt,"%%((%%w)%%)","%%1")
    end
    TriggerStyleRuns[i].text = '[' .. TriggerStyleRuns[i].txt .. ']'
end
编辑:

为了从
“(K)(M)(G)”等格式中提取文本,我将这样做:

-- Assuming 'flags' is the specific string
local str = '['
flags:gsub( '%((.)%)', function( flag )
    str = str .. flag
end )
str = str .. ']'
编辑2:

读了你最新的描述后,我对你的目标有了更好的了解。看起来最好的方法还是事先做好,但是如果你一直这样做,我想我有个主意

由于看起来您正在修改它们所在的表(而不是创建一个新表),这会使事情变得更复杂,但仍然可行

local flags = { K = true, M = true, G = true, H = true }

TriggerStyleRuns = {
  {backcolour=0, text="( 7) ", length=5, style=1, textcolour=16777215}, 
  {backcolour=0, text="(K)", length=3, style=1, textcolour=255},
  {backcolour=0, text="(M)", length=3, style=1, textcolour=16711680},
  {backcolour=0, text="(G)", length=3, style=1, textcolour=16777215},
  {backcolour=0, text="(H) ", length=4, style=1, textcolour=16776960},
  {backcolour=0, text="a ", length=2, style=0, textcolour=12632256},
  {backcolour=0, text="Bag of ", length=7, style=1, textcolour=65535},
  {backcolour=0, text="Aardwolf", length=8, style=1, textcolour=255},
  {backcolour=0, text=" ", length=1, style=0, textcolour=12632256},
  {backcolour=0, text="(", length=1, style=1, textcolour=16777215},
  {backcolour=0, text="72", length=2, style=1, textcolour=65280},
  {backcolour=0, text=")", length=1, style=1, textcolour=16777215}
}

local function defaultText( str )
    return {backcolour=0,text=str, length=#str, style=1, textcolour=1234567}
end

local matchStarted -- nil
local i = 0
-- Can't use for loop because we're inserting elements into the same table
while matchStarted ~= false do
    i = i + 1
    local text = TriggerStyleRuns[i].text:match( '%((.)%)' )
    if flags[text] then
        if not matchStarted then
            matchStarted = true
            table.insert( TriggerStyleRuns, i, defaultText( '[' ) )
            i = i + 1 -- Don't want to process a letter twice
        end
        TriggerStyleRuns[i].text = text
        TriggerStyleRuns[i].length = #text
    elseif matchStarted then
        matchStarted = false
        table.insert( TriggerStyleRuns, i, defaultText( ']' ) )
    end
end

不,正如我所提到的,无论存在哪个标志,字母都会按照那个顺序出现。因此,如果缺少标志
H
I
,它将显示
(K)(M)(G)
,而如果它们都在,它将显示
(K)(M)(I)(G)(H)
。@Josh你是说你希望输出是这样的,还是你已经有这样的输出?如果是后者,您要查找的是什么?输出是
(K)(M)(I)(G)(H)
(或实际存在的任何标志)。我想做的是把
(K)(M)(I)(G)(H)
或列出的任何东西作为
[KMGH]
返回(括号将根据我的存在而着色)。作为旁注,我想你至少无意中给了我一个更好的方法。@Josh我更新了我的答案,这就是你的意思吗?