elist in IRE muds

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

elist in IRE muds

Post by naftali »

I've started writing my own piece of code to take the output of the elist2 and turn it into a table displaying how many sips of each type of elixer and salve I have. So far I've successfully gotten it to capture and suppress the normal output of elist2 while creating a table 'vials' that has entries that look like this:

Code: Select all

  '333606': table {
    'elixer': 'an elixir of mana'
    'sips': '121'
    'decay': '66'
    }
Also, it prints out the following to act as the header for the new table I want to create:

Code: Select all

  Elixer/Salve                 Number of Sips
-----------------------------------------------------
Finally, the trigger is enabled/disabled when it should be and doesn't capture more lines than it should.

My question is this - how can I take my 'vials' table and turn it into an 'elixers' table which has entries that look like

Code: Select all

"an elixer of mana" = 723
723, in this example, is the total number of sips left in vials that contain mana elixer.

I then wanna print those into the table as shown above:

Code: Select all

  Elixer/Salve                 Number of Sips
-----------------------------------------------------
  "an elixer of mana"       723
I figure I need to use "for" to do this somehow, but I have never successfully used "for", nor have I found any documentation that really lets me understand the correct syntax. Also, if there's some elegant way for me to make it say "Mana" instead of "an elixer of mana" I would want to do that, but that is far less important than the rest.

Sorry for the long winded post! Anybody have any helpful hints?

kaeus
Posts: 50
Joined: Thu Dec 31, 2009 4:33 pm

Re: elist in IRE muds

Post by kaeus »

Code: Select all

echo("Elixer/Salve                 Number of Sips")
echo("-----------------------------------------------------")
for vnum, details in pairs(vials) do
     if details["elixer"] ~= nil then
          echo(details[1] .. string.repeat(20 - #details[1]) .. details[2] .. "\n")
     end
end
That may do the trick if I understand your vials variable correctly. Also, you can adjust the 20 to determine where the number of sips starts.

hempa
Posts: 48
Joined: Sat Jan 02, 2010 1:07 pm

Re: elist in IRE muds

Post by hempa »

What I did for my elixlist code was to write a function that parses the long name of the elixir ("an elixir of health", "a restoration salve", "the toxin aconite" etc) into its shorter form ("health", "restoration", "aconite").

Here's a working solution for such a function:
Code: [show] | [select all] lua
function getElixirName( long )
   
   local elixPatterns = {
      "an elixir of (%a+)",
      "a (%a+) salve",
      "a salve of (%a+)",
      "the toxin (%a+)"
   }
   
   for _,pat in ipairs(elixPatterns) do
      local m = string.match( long, pat )
      if m then return m end
   end
   return nil
end
Hope this helps!

hempa
Posts: 48
Joined: Sat Jan 02, 2010 1:07 pm

Re: elist in IRE muds

Post by hempa »

Also for the echoing, why not use string.format?

echo(string.format("%-25s %-20s","Elixir/salve","Number of sips"))
echo("-----------------------------------------------------")
for vnum, details in pairs(vials) do
if details["elixer"] ~= nil then
echo(string.format("%-25s %-5d", getElixirName( details["elixer"] ), details["sips"]) )
end
end

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: elist in IRE muds

Post by naftali »

@hempa - That's a great function you wrote there for me, thanks a million! Also string.format() is something I hadn't thought to use, but it looks great.

@kaeus - Your code is causing an error, saying that a " is expected near 'repeat'. I would try and fix it if I had a better idea of what your code did.

I want to be more helpful and fool around with this on my own (in my experience the easiest way to get what I want) but for some reason even the simplest applications of this escape me. For example:

Code: Select all

for vnum, details in pairs(vials) do
     echo(details.elixer)
end
only echos details.elixer for the first value in the table, when I thought the whole point of 'for' was that it would work for ALL values in the table. Simple help, please? If I can figure out how to get this working I can probably get the rest working on my own.

hempa
Posts: 48
Joined: Sat Jan 02, 2010 1:07 pm

Re: elist in IRE muds

Post by hempa »

@naftali - going by the layout of your table, it should look something like what is in the box below.
Code: [show] | [select all] lua
vials = {
 ['333606'] = { elixer = 'an elixir of mana', sips = '121', decay = '66' },
 ['526232'] = { elixer = 'an elixir of health' sips = '51', decay = '152' },
}
What the pairs function does is return, from the vials table, the key (in your case, the vial number) and the value, which is a table filled with all your properties. If put in a loop, it will iterate over the array one record at a time.

The only reason I could see why it wouldn't print anything but the first line is that the table contains only that for some odd reason. Could you do display(vials) just before you do the loop and check what you have there?

Cheers

kaeus
Posts: 50
Joined: Thu Dec 31, 2009 4:33 pm

Re: elist in IRE muds

Post by kaeus »

naftali wrote:@kaeus - Your code is causing an error, saying that a " is expected near 'repeat'. I would try and fix it if I had a better idea of what your code did.
I did this without running it in Mudlet...my bad.

Change

Code: Select all

string.repeat(20 - #details[1])
to

Code: Select all

string.repeat(" ",20 - #details[1])
and the error should go away.

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: elist in IRE muds

Post by naftali »

@hempa - I did do display(vials) and it looks correct - each vial number has a table with 3 key-value pairs in it just like you posted. Something worth mentioning perhaps is that elixer, sips, and decay are all strings. Not sure if that makes a difference.

@kaeus - just got my computer back from the shop to test what you gave me, and the error is still there after I made your correction. I've uploaded the trigger, alias, and script that I use for this thing so anybody still interested in lending a hand can have a look at it.
Attachments
Elist.xml
(5.79 KiB) Downloaded 339 times

hempa
Posts: 48
Joined: Sat Jan 02, 2010 1:07 pm

Re: elist in IRE muds

Post by hempa »

Well, in that case I would suggest you use the debugging feature to see if you have an error somewhere. If you for some reason have a nil value in your table that you try to echo, then it will fail because it can't concatenate a nil value.

Personally, I'd use the named position instead of the numbered for this as well, it will help you figure things out.
Code: [show] | [select all] lua
		echo(details[1] .. string.repeat(" ",20 - #details[1]) .. details[2] .. "\n")
Changed into this:
Code: [show] | [select all] lua
		echo(details.elixer .. string.repeat(" ",20 - #details.elixer) .. details.sips .. "\n")

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: elist in IRE muds

Post by naftali »

Aha! Finally, that works! Maybe eventually I'll figure out a way to control what order the elixers are listed in.

@hempa Thanks so much! Just fyi though the function is string.rep() not string.repeat() at least in the latest dev version of Mudlet.

Post Reply