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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  String Manipulation

String Manipulation

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


Pages: 1 2  3  4  

Posted by Kevnuke   USA  (145 posts)  [Biography] bio
Date Mon 16 Jan 2012 01:01 AM (UTC)
Message
This may seem very simple but how do I remove a single character from the middle of a string?

Char.Vitals --> CharVitals
Comm.Channel.Start --> CommChannelStart

or substitute one part of that for another?

CharacterName = "Jim"

Char.Vitals { "hp": "3000", "maxhp": "3000", "mp": "2000", "maxmp": "2000"}

-->

Jim.Vitals = {"hp" = 3000, "maxhp" = 3000, "mp" = 2000, "maxmp" = 2000}

like that?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Mon 16 Jan 2012 06:00 AM (UTC)
Message
See string.gsub

http://lua-users.org/wiki/StringLibraryTutorial

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Kevnuke   USA  (145 posts)  [Biography] bio
Date Reply #2 on Thu 26 Jan 2012 06:44 AM (UTC)
Message
I think i figured it out.


char = "Jim"

_G[char] = {}

do

local n = _G[char]

n.r = 15

end

print (_G[char].r)
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Thu 26 Jan 2012 09:06 AM (UTC)
Message
Huh?

- Nick Gammon

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

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #4 on Fri 27 Jan 2012 05:49 PM (UTC)
Message
Kevnuke said:
I think i figured it out.


The code you just posted doesn't make any sense.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Kevnuke   USA  (145 posts)  [Biography] bio
Date Reply #5 on Sun 29 Jan 2012 11:48 PM (UTC)

Amended on Sun 29 Jan 2012 11:50 PM (UTC) by Kevnuke

Message
I did figure out my original problem though.


msg = Char.Vitals

msg:gsub ("%.", "")

-->

CharVitals


isn't that right?

As for the second part. I was trying to make a different table for each of my characters using GMCP data.


local _G[CURRENT_CHARACTER] = {}

local VITALS = rex.new ('{ "hp": "%d+", "maxhp": "%d+", "mp": "%d+", "maxmp": "%d+", "ep": "%d+", "maxep": "%d+", "wp": "%d+", "maxwp": "%d+", "nl": "%d+", "string": "H:(?<health>%d+)/(?<healthmax>%d+) M:(?<mana>%d+)/(?<manamax>%d+) E:(?<endurance>%d+)/(?<endurancemax>%d+) W:(?<willpower>%d+)/(?<willpowermax>%d+) NL:(?<levelprogress>%d+)/100" }')

function CharVitals (content)

  local t = _G[CURRENT_CHARACTER]
  s, e, t.stats = VITALS:gmatch(content)

end

print (_G[CURRENT_CHARACTER].stats.health)

-->

Compile Error


Is there something wrong with this?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #6 on Mon 30 Jan 2012 12:26 AM (UTC)
Message
Quote:
Is there something wrong with this?
If you get an error then, yes, there is obviously something wrong. ;)
Luckily the error message should tell you what it is.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #7 on Mon 30 Jan 2012 02:37 AM (UTC)

Amended on Mon 30 Jan 2012 02:38 AM (UTC) by Twisol

Message
Kevnuke said:
local _G[CURRENT_CHARACTER] = {}

This line is incorrect. You don't use 'local' when you're adding an entry to an table; 'local' is just to specify whether the variable you're creating is accessible from everywhere or just in the current scope.

Incidentally, since _G contains all of the global variables, it doesn't make much sense to declare a 'local global' anyways.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Kevnuke   USA  (145 posts)  [Biography] bio
Date Reply #8 on Mon 30 Jan 2012 03:48 AM (UTC)

Amended on Mon 30 Jan 2012 03:53 AM (UTC) by Kevnuke

Message
Yea that does look pretty funny now that I think about it.

I've been trying different things to get that pattern to match the test string. Adding backslashes and such to see if that's why it isn't returning any values to the table t. I'm gonna start with the pattern .+ and add things to it, one at a time.

If I were doing this in a trigger it would be way easy for me haha.

EDIT: spelling error
[Go to top] top

Posted by Kevnuke   USA  (145 posts)  [Biography] bio
Date Reply #9 on Mon 30 Jan 2012 11:34 PM (UTC)

Amended on Tue 31 Jan 2012 12:23 AM (UTC) by Kevnuke

Message
Ok this is pretty ugly..but it works at least


VITALS = rex.new ('\{ \"hp\"\: \"....\"\, \"maxhp\"\: \"....\"\, \"mp\"\: \"....\"\, \"maxmp\"\: \"....\"\, \"ep\"\: \".....\"\, \"maxep\"\: \".....\"\, \"wp\"\: \".....\"\, \"maxwp\"\: \".....\"\, \"nl\"\: \".+\"\, \"string\"\: \"H\:(?<health>....)/(?<healthmax>....) M\:(?<mana>....)\/(?<manamax>....) E\:(?<endurance>.....)\/(?<endurancemax>.....) W\:(?<willpower>.....)\/(?<willpowermax>.....) NL\:(?<levelprogress>.)\/100 \" \}')


I can't get the regular special characters to work in place of the dot character. \d \s \w etc. is it %d %s %w ?

EDIT:
I got it.

In a rex pattern match you have to double escape special matching characters \d \s \w etc. Although it's kinda stupid, why was it made that way?


VITALS = rex.new ('\{ \"hp\"\: \"\d+\"\, \"maxhp\"\: \"\d+\"\, \"mp\"\: \"\d+\"\, \"maxmp\"\: \"\d+\"\, \"ep\"\: \"\d+\"\, \"maxep\"\: \"\d+\"\, \"wp\"\: \"\d+"\, \"maxwp\"\: \"\d+\"\, \"nl\"\: \"\d+\"\, \"string\"\: \"H\:(?<health>\d+)/(?<healthmax>\d+) M\:(?<mana>\d+)\/(?<manamax>\d+) E\:(?<endurance>\d+)\/(?<endurancemax>\d+) W\:(?<willpower>\d+)\/(?<willpowermax>\d+) NL\:(?<levelprogress>\d+)\/100 \" \}')
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Tue 31 Jan 2012 05:16 AM (UTC)
Message
Lua regular expressions use % for that very reason. If you are using the PCRE ones inside Lua strings, and since Lua strings use backslash for special characters (like newline) then you have to double them.

The trouble is, the inventors of regular expressions thought "I'll use a backslash to 'escape' special characters - no-one uses them for much else".

And so did the inventors of the C language. And the Lua language. And so on.

So now you get "backslash confusion".

- Nick Gammon

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

Posted by Kevnuke   USA  (145 posts)  [Biography] bio
Date Reply #11 on Wed 01 Feb 2012 10:33 PM (UTC)
Message
Why did the forum remove the leading backslashes from my post? -.-
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #12 on Wed 01 Feb 2012 11:14 PM (UTC)
Message
Kevnuke said:

Why did the forum remove the leading backslashes from my post? -.-


You may as well ask why "\n" doesn't print \n. :P The backslash is how the forum software determines whether you want foo[i] = "stuff" or foo = "stuff".

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Kevnuke   USA  (145 posts)  [Biography] bio
Date Reply #13 on Sat 04 Feb 2012 04:19 AM (UTC)
Message
I still have this problem, although i figured out the other problem.


char = "Jim"

_G[char] = {}

do

local n = _G[char]

n.r = 15

end

print (_G[char].r)


This works, but if you add another field in depth it doesn't..


char = "Jim"

_G[char] = {}

do

local n = _G[char]

n.r.a = 15

end

print (_G[char].r.a)

-->

Run-time error


Can anyone tell me a way around this?
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #14 on Sat 04 Feb 2012 04:31 AM (UTC)
Message
I don't understand what you are doing here, at all.

You shouldn't be storing your own variables in the global variable space. eg.


char = "Jim"

_G[char] = {}


Is basically:


 Jim = {}


What if you have a character one day called "math" ? Then all your math functions go away.

You should be storing game-related stuff in your own table, not _G.


print (_G[char].r.a)


That is getting "a" from table "r" from the item indexed by char in table _G. That will fail if "r" is not a table.

And in your example "r" does not even exist, let alone be a table.

- 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.


98,457 views.

This is page 1, subject is 4 pages long: 1 2  3  4  [Next page]

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 HostDash]