Removing a string in a variable with a space in it.

Posted by Wuggly on Sat 20 Feb 2016 12:16 AM — 5 posts, 19,631 views.

USA #0
Hi,
I'm trying to find a way to remove a specific variable in a string that has a space in it. The variable doesn't always exist.

Working with msdp, in the affects string I have a variable that sometimes shows up as...

enchant weapon[-1


Right after the weapon part there is this tiny upside-down L that I can't seem to copy and paste.

The string is made like this.

affects = msdp["AFFECTS"]


I'm trying to figure out a way to delete that "enchant weapon" variable out of the string if it shows up.

The part that confuses me is the space in it... and since it's not a table the -1 part.

EDIT: Apparently when I tried pasting in that tiny upside-down L, it showed up as a beginning bracket after posting. When writing the post though it showed nothing (not even a space), and same when editing. So just imagine that bracket as a tiny upside-down L.

EDIT: It also shows up twice. So both need to be removed. Here's what it looks like when I have a couple other spells up.

enchant weapon[-1\enchant weapon[-1\improved invis[16\shield[33


EDIT: Ok.. I can't seem to paste the weird code right, cause now it's showing backslashes and when posting it doesn't show those, they are supposed to be spaces. Maybe just msdp is weird.

Anyways, here's a screenshot.
http://prntscr.com/a5j9a9
Amended on Sat 20 Feb 2016 12:45 AM by Wuggly
Australia Forum Administrator #1
Ghostery blocked your image site.

Anyway, what sort of variable? Lua variable or MUSHclient variable?

And what do you mean by "remove a variable"?


affects = nil


That will "remove" the variable "affects".
USA #2
Fixed subject title. I meant removing those words out of the string in that affects variable. Hopefully I'm saying it right now.

Here's the entirety of the affects code.


local AffectName = {}
local AffectDuration = {}
local AffectMax = 0

function init_affects (data)

  index = 0
  startpos = 1
  max = 0
  for i = startpos, string.len(data), 1 do
    if string.byte(data,i) == 1 or i == string.len(data) then
      if string.byte(data,i) == 1 then
        endpos = 1
      else
        endpos = 0
      end -- if
      variable = string.sub(data,startpos,i-endpos)
      startpos = i+1
      index = index + 1

      pos1 = string.find(variable, "\002")
      if pos1 ~= nil then
        AffectName[index] = string.sub(variable, 1, pos1-1)
        AffectDuration[index] = string.sub(variable, pos1+1)
      end -- if
    end -- if
  end -- for

  -- AffectMax is the highest EVER number - we need to keep track of all created icons
  if index < AffectMax then
    for i=index+1,AffectMax,1 do
      AffectName[i] = nil
      win = "affect_window_"..i
      WindowShow (win, false)
    end -- for
    AffectMax = index
  elseif index > AffectMax then
    AffectMax = index
  end -- if

end -- function



function draw_affects ()

  affects = msdp["AFFECTS"]
  if affects == nil or affects == "None" then
    for i=1,AffectMax,1 do
      WindowShow ("affect_window_"..i, false)
    end -- for
    AffectMax = 0
    return
  end -- if

  init_affects (affects)

  offset_x = 0
  offset_y = 0

  for i=1,AffectMax,1 do
    if AffectName[i] ~= nil then
      win = "affect_window_"..i
      affect = "affect_"..i
     
       -- draw the icons left to right, top to bottom
      if i > 1 then
        if offset_x == 0 then
          offset_x = 36
        elseif offset_x == 36 then
          offset_x = 72
        elseif offset_x == 72 then
          offset_x = 108
        else
          offset_x = 0
          offset_y = offset_y + 36      
        end -- if
      end -- if

      colour = colourWhite


Rest of this function is just it creating a miniwindow, loading icon images, and adding hot spots. Here's the loading image part of it if it helps any.


 if WindowLoadImage (win, affect, GetInfo (66) .. "Generic/affects/" .. AffectName[i] .. ".png") == eOK then
        check (WindowDrawImage (win, affect, 1, 1, 33, 33, 2))  -- draw the icon
     elseif WindowLoadImage (win, affect, GetInfo (66) .. "Generic/affects/default.png") == eOK then
        check (WindowDrawImage (win, affect, 1, 1, 33, 33, 2))  -- draw the default icon instead
      else -- even the default spell icon is missing
        Note( "Missing spell icons.")
        return
      end -- if


The AffectName[eye] part is actually the letter i. I had to make it that way so it wouldn't make it that italic code when posting.

So when msdp["AFFECTS"] puts that enchant weapon part into the affects string, I need to somehow remove it. Would gsub work for something like this? Maybe string.match for enchant weapon then remove it with gsub like "" ? Doing it like that I suppose it would be easy to match the space, but what about that tiny upside-down L and the -1 part? Sorry for this confusing question.

Here's the screenshot on imgur, hopefully that one isn't blocked for you.
http://i.imgur.com/Vf79m7y.png


[EDIT] Escaped square brackets.
Amended on Sat 20 Feb 2016 09:11 PM by Nick Gammon
Australia Forum Administrator #3
You can fix the italics by "escaping" the brackets. I did it for you. See Edit menu (in MUSHclient) -> Convert Clipboard Forum Codes.




Your code looks very complex. You can easily remove something from a string like this:


test = "foo|bar"
test2 = string.gsub (test, "|", " ")  -- remove something
print (test2)  --> foo bar


If you want to remove from something onwards, do this:


test = "foo|bar"
test2 = string.gsub (test, "|.*", "")  -- remove rest of line
print (test2)  --> foo


If you want to copy up to the first space:


test = "sword with enchants"
test2 = string.match (test, "^%S+")  -- copy up to space
print (test2)  --> sword
Australia Forum Administrator #4
Or, looking at your screenshot, maybe this:


test = "sword with 9 enchants+rubbish"
test2 = string.match (test, "^[%s%a%d]+")  -- copy spaces, letters, numbers
print (test2)  --> sword with 9 enchants


That lets through initial spaces (%s) letters A-Z (%a) and digits (%d).