Wildcards, Functions & Lua Tables

Posted by Mccane on Tue 06 May 2008 11:32 AM — 2 posts, 15,218 views.

#0
Okay, on the MUD I am playing you have a set of skills. For example, you might see something like

Alchemy: 10% Engineering: 5%
Herbalism: 8% Tailoring: 32%


and on and on, up to about 60 skills. I would like to store each of the skills with their respective percentage points into Lua tables, upon which my script will 'rank' each of these skills into prioritized lists of skills I need to improve (i.e. it will find skills below x% and add it to a list, then when it has iterated throughout the entire table will print the list to me, in order from the lowest to highest.)

I wrote this trigger to capture each skill:

<triggers>
  <trigger
   enabled="y"
   match="(.*?) (.*?[0-9.])\%"
   name="skill"
   regexp="y"
   script="skill_function"
   sequence="100"
  >
  </trigger>
</triggers>

Here is the skill_function:

function skill_function (name, line, wildcards)

  local function skill (m, t)
  table.insert (skills, m)
  end
  local re = rex.new (GetTriggerInfo (name, 1))
  re:gmatch (line, skill)

end

This will insert every skill to the table nicely, but I'm not quite sure where to proceed from here. I'm fairly sure my method of storing the skills is incorrect for what I want to do. How can I take 'm', what the trigger matched on, and break that down into both the skill name and its percentage? I tried using named expressions and then calling these in the function. I changed my match to:

(?P<skill>.*?)\: (?P<level>.*?[0-9.])\%


and then tried accessing these in the function, but it then would no longer store all the skills. I changed the function to look like this:

skills [wildcards.skill] = wildcards.level


This works somewhat, but it only stores the skills in the first column. Given the above example of skills, I would get something like this:

/table.foreach (skills, print)
Alchemy 10
Herbalism 8


Is there some easier way to go about this?
Amended on Tue 06 May 2008 11:48 AM by Mccane
USA #1
Allow the regex trigger to continue matching on the line... or allow a regex with potentially multiple skills on each line grabbing them, then test for existence before adding a new item to the table.

make match:

(?P<skill>.*?)\: (?P<level>.*?[0-9.])\%(?:\s+(?P<skill2>.*?)\: (?P<level2>.*?[0-9.])\%)?


Then have this code:

skills [wildcards.skill] = wildcards.level
if wildcards.skill2 then
  skills [wildcards.skill2] = wildcards.level2
end