Editing a table with wildcards

Posted by Serris on Sun 13 Feb 2011 09:55 AM — 3 posts, 13,273 views.

#0
I am making a limb counter for Achaea and I have a bunch of functions but one of them I need help with. To start, it uses this table:

bodypart = {
  head = 0,
  torso = 0,
  rightarm = 0,
  leftarm = 0,
  rightleg = 0,
  leftleg = 0,
  }


The function looks like this:

function limbhit(sTrig, sLine, wildcs) -- adds damage to limbs. 1 for punch, 2 for kick

  bodypart.wildcs[1] = (bodypart.wildcs[1] or 0) + 1
  
  if GetVariable("attack") == "kick" then
    bodypart.wildcs[1] = bodypart.wildcs[1] + 1
  end

end -- function


One of my triggers fires on "^You connect to the (.+)\.$ The (.+) could be head, torso, left leg, right leg, left arm, right arm. Since I cannot have a space between left and arm or left and leg in the table (left leg gives me an error), I need some way of combining those two words. I could do:

bodypart.wildcs[2]wildcs[2] = (bodypart.wildcs[1]wildcs[2] or 0) + 1

but that will only work if there are two wildcards in the trigger, and there isn't always. Is there anyway I could do something with gsub and then edit the table or is there an easier way to take care of this?
Australia Forum Administrator #1
Doing this:


bodyparts.foot = "foo"


is "syntactic sugar" for:


bodyparts ["foot"] = "foo"



So, extending this, whilst this gives an error:


bodyparts.left leg = "foo"


This won't:


bodyparts ["left leg"] = "foo"



So, just change the way you access the table.
#2
Hah, awesome. Thank you much!