質問

  • Current Output of table mvp, if player1 was alive:

    "player1, player2, player3, player1, player2, player1, player2, player3, player1"

  • Desired Output of table mvp, if player1 was alive:

    "player1 - 4, player2 - 3, player3 - 2"

I'm asking on how to shorten the output. @Yu Hao, Great Work! It's just I was bad at explaining my problem ): Sorry I edited my problem


役に立ちましたか?

解決

For the edited question, you can store the table items in a set with their count like this:

mvp = {"player1", "player2", "player3", "player1", "player2", "player1", "player2", "player3", "player1"}

local t = {}
for k, v in ipairs(mvp) do
    t[v] = (t[v] or 0) + 1
end
local count = {}
local index = 1
for k, v in pairs(t) do
    count[index] = k .. " - " .. v
    index = index + 1
end

str = table.concat(count, ", ")
print(str)

Output: player1 - 4, player3 - 2, player2 - 3

Note that the names are not sorted, because of the usage of pairs(), you need some extra work to do if the names need to be sorted.

他のヒント

Try this: you can directly use the mvp and mvpp tables to count the # of occurrences; then use the string.format to format output as you wish, but since there can be several players, you then use table.concat to combine output of several players; finally you put in ui:

function output(scores, x, y)
    local out  = {}
    for player, score in scores do 
        table.insert(out, string.format("%s - %s", player, score))
    end
    ui.addTextArea(1,"<a href='event:closee'> ".. table.concat(out, ";") .. " </a>", NIL, 6, x,y, 50,0x1C3C41,0x1C3C41,0.9,true)
end

function eventLoop(time)
    local mvp = {}
    local mvpp = {}

    if time < 120000 then
        for name, player in pairs(tfm.get.room.playerList) do
            if not player.isDead then
                if TeamOne[name] then
                    mvp[name] = (mvp[name] or 0) + 1
                end
                if TeamTwo[name] then
                    mvpp[name] = (mvpp[name] or 0) + 1
                end
            end
        end
    end
    -- now mvp = {player1 = 200, player3 = 50} for example
    output(mvp,    6, 308)
    output(mvpp, 406, 208)
end

There's actually a couple more parameters to pass to output() (like first parameter to addTextArea() and href name) but you get the idea.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top