Pluralising Variables

Posted by Neves on Tue 19 Jul 2011 12:26 PM — 7 posts, 32,663 views.

USA #0
I'm looking for a way to have an alias take the value in a variable, say @var, and create @varplural with it in plural form.

Since English plurals are complicated, I need it to check if the last consonant in @var is an s, if it is add es to the end, if not just add s. Also there are the words that are both plural and not, how would I write an exception list for the alias? Like exclude (boots, pants, trousers, etc?).

Thanks,
Neves
USA Global Moderator #1
probably the simplest way is to construct a table with the form

exceptions_table = {boots=true, pants=true, trousers=true}

And then check for exceptions by doing
if exceptions_table[@var] then
Australia Forum Administrator #2
Inside an alias you actually want:


if exceptions_table["@var"] then


And if you have just set that variable in the same alias this is needed:



if exceptions_table[GetVariable ("var")] then

USA #3
OK, and how would I check a string to see if the last letter is an 's' or not to decide if the plural is to add an 's' or 'es' (i.e. glass should be changed to glasses, chair would be changed to chairs).

Also, does anyone know if such a list of exceptions to the plural rule exists anywhere, or do I have to write my own?

Third, if I have a variable in a plugin, does it save whatever information it was changed to after I loaded it it even if I close the plugin and reload it, or send the plugin to someone else?

Thanks,
Neves
USA #4
Neves said:
OK, and how would I check a string to see if the last letter is an 's' or not to decide if the plural is to add an 's' or 'es' (i.e. glass should be changed to glasses, chair would be changed to chairs).


if string.sub("@var", -1) == "s" then
  -- ...
end


Neves said:
Third, if I have a variable in a plugin, does it save whatever information it was changed to after I loaded it it even if I close the plugin and reload it

Yes, if you have save_state="y" in your plugin's <plugin> tag. This only works for MUSHclient variables (i.e. GetVariable/SetVariable), not Lua script variables.

Neves said:
, or send the plugin to someone else?

Not exactly. The plugin's saved state is stored separately (you can view it by double-right-clicking the plugin in the plugins list). If you want to send that along with the plugin, you can.
Amended on Thu 21 Jul 2011 04:49 AM by Twisol
USA #5
I want to do this within the MUSHClient alias system, does the if string.sub work inside an alias or only for a plugin? Also how would I have it match the last word in a string instead of just the last letter?

Neves
Australia Forum Administrator #6
Neves said:

Third, if I have a variable in a plugin, does it save whatever information it was changed to after I loaded it it even if I close the plugin and reload it, or send the plugin to someone else?


As Twisol said, if you have save_state set to "y" then MUSHclient variables are saved, per world. This isn't fabulously useful for sending to someone else because they'll have different world file IDs.

For distributing data, a couple of options would be:

  • Serialize the variables into a string (see: http://www.gammon.com.au/forum/?id=4960) and then write that string to a text file (you could open it with io.open, write to it, close it).
  • Use a SQLite3 database (see: http://www.gammon.com.au/db). This lets you share things (eg. mob names) between multiple worlds as the same database could be opened by many worlds. Also you could send the database to someone else.


Neves said:

I want to do this within the MUSHClient alias system, does the if string.sub work inside an alias or only for a plugin?


Yes, the Lua libraries work inside aliases if you set the language to Lua and do "send to script".

Neves said:

Also how would I have it match the last word in a string instead of just the last letter?


One way is to use a regular expression, and find a word (ie %a+) anchored to the end of the target string, like this:


test = "Every good boy deserves fruit"
last_word = string.match (test, "%a+$")
print (last_word)  --> fruit


Note that since the % symbol has a special meaning inside alias "send" boxes, you need to double it if you are using send to script, ie.


last_word = string.match (test, "%%a+$")