1 Michael - Name 2343 - Gold
2 Loo - Name 256 - Gold
3 James - Name 405 - Gold
4 Sam - Name 1202 - Gold
5 Beth - Name 0 - Gold
6 Hogan - Name 0 - Gold
7 Alan - Name 0 - Gold
8 John - Name 0 - Gold
9 Tammy - Name 0 - Gold
10 Elvin - Name 0 - Gold
How do i ignore the 0 values and send the rest to a channel listing from biggest number to smallest and list the percentage for each of them?
wanted result:
Michael (55.70%) , Sam (28.57%), James (9.63%), Loo (6.08%)
-- test data
members = {
"Michael - Name 2343 - Gold",
"Sam - Name 1202 - Gold",
"James - Name 405 - Gold",
"Loo - Name 256 - Gold",
"John - Name 0 - Gold"
}
myList = { } -- table for outputting
totalGold = 0
-- find total gold
for k, v in ipairs (members) do
name, gold = v:match ("^([^ ]+) %- Name (%d+) %- Gold")
if gold then
totalGold = totalGold + gold
end -- if
end -- for
-- make table of people who have more than zero gold
for k, v in ipairs (members) do
name, gold = v:match ("^([^ ]+) %- Name (%d+) %- Gold")
if name and tonumber (gold) ~= 0 then
table.insert (myList, string.format ("%s(%0.2f%%)", name, gold / totalGold * 100))
end -- if they have some gold
end -- for
-- send results
Send ("chat " .. table.concat (myList, ", "))
Hi Nick! Thanks again! I tried put this code into alias and run it but it occurred an error. Can this work from alias?
It also did not work when i replaced table.foreach (members, print) in the last trigger with it. :(
<aliases>
<alias
match="tabletest"
enabled="y"
group="TEST"
send_to="12"
sequence="100"
>
<send>-- test data
members = {
"Michael - Name 2343 - Gold",
"Sam - Name 1202 - Gold",
"James - Name 405 - Gold",
"Loo - Name 256 - Gold",
"John - Name 0 - Gold"
}
-- find total gold
for k, v in ipairs (members) do
name, gold = v:match ("^([^ ]+) %- Name (%d+) %- Gold")
if gold then
totalGold = totalGold + gold
end -- if
end -- for
-- make table of people who have more than zero gold
for k, v in ipairs (members) do
name, gold = v:match ("^([^ ]+) %- Name (%d+) %- Gold")
if name and tonumber (gold) ~= 0 then
table.insert (myList, string.format ("%s(%0.2f%%)", name, gold / totalGold * 100))
end -- if they have some gold
end -- for
Ah yes, well inside a trigger or alias the % symbol has a special meaning, eg. %1 is wildcard 1 and %2 is wildcard 2 and so on. To literally put a % inside (as I did in the string.format) you need to double it, so this works:
<aliases>
<alias
match="tabletest"
enabled="y"
group="TEST"
send_to="12"
sequence="100"
>
<send>-- test data
members = {
"Michael - Name 2343 - Gold",
"Sam - Name 1202 - Gold",
"James - Name 405 - Gold",
"Loo - Name 256 - Gold",
"John - Name 0 - Gold"
}
myList = { } -- table for outputting
totalGold = 0
-- find total gold
for k, v in ipairs (members) do
name, gold = v:match ("^([^ ]+) %%- Name (%%d+) %%- Gold")
if gold then
totalGold = totalGold + gold
end -- if
end -- for
-- make table of people who have more than zero gold
for k, v in ipairs (members) do
name, gold = v:match ("^([^ ]+) %%- Name (%%d+) %%- Gold")
if name and tonumber (gold) ~= 0 then
table.insert (myList, string.format ("%%s(%%0.2f%%%%)", name, gold / totalGold * 100))
end -- if they have some gold
end -- for
-- send results
print ("chat " .. table.concat (myList, ", "))</send>
</alias>
</aliases>
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.
I changed Send to print near the bottom but you can change that back when you are happy it works.