[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Removing a string in a variable with a space in it.
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

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

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Sat 20 Feb 2016 12:16 AM (UTC)

Amended on Sat 20 Feb 2016 12:45 AM (UTC) by Wuggly

Message
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
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sat 20 Feb 2016 04:14 AM (UTC)
Message
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".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #2 on Sat 20 Feb 2016 09:27 AM (UTC)

Amended on Sat 20 Feb 2016 09:11 PM (UTC) by Nick Gammon

Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sat 20 Feb 2016 09:17 PM (UTC)
Message
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

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sat 20 Feb 2016 09:29 PM (UTC)
Message
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).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


2,304 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]