Trying to set up tables

Posted by Dexodro on Mon 01 Sep 2008 05:57 AM — 8 posts, 31,068 views.

#0
Hello.

To get straight into what I'm trying to do, I'll start by throwing my script :


^(\w+)(?:(\s+))(?:\(|\(house\))(.+)\)$

script :
who.name.%1 = "%1"
who.name.%1.where = "%3"


Mind you, the RegEx is sloppy, and the tables aren't precisely what I'd like, but I have to stick with it. (Can't get %3 to get a table named after itself because it sometimes consists of more than one word and/or other characters).

Essentially, this is just a script to gather everyone from a WHO list and their location, and place them altogether. With that, I'm hoping with enough time, that I can output location names (will show example below) and everyone who is in that specific room.

-Roomname1- : Name1, Name2, Name3, Name4
-Roomname2- : Name5

Et cetera. This is going to take some work on my part, understandably, but, I think it's good practice, and upon finding an answer, should better my skills with Lua.

I'll be in debt to whomever can help me.

Thanks in advance,
BDKL
Australia Forum Administrator #1
Quote:

... and upon finding an answer ...


Sorry, I couldn't see a question there.
Australia Forum Administrator #2
If you are referring to the bit about the "more than one word", these are equivalent:


who.name.nick

who.name ["nick"]


Thus you can put anything you like in, if you quote it, eg.


who.name ["the quick brown fox jumps over the lazy dog"] = "hungrily"

#3
Sorry. I'm half-asleep, and trying to focus. Guess I lost myself.

What I meant was, every time I try to run this trigger :


[string "Trigger: "]:2: attempt to index field 'NameHere' (a string value)
stack traceback:
        [string "Trigger: "]:2: in main chunk


This is the error message I get every time.
I'm not sure what I'm doing wrong, though.

I've never done any coding beyond Qbasic as a kid, right? So this is still all new to me. I've learned what I could, and am still reading some of the topics, but interaction has always been the best teacher.
So, my questions are :

(a) How can I get the tables to name themselves after the %1 and %3 areas?
(b) Upon doing that, is there any way I can place %1 in the corresponding %3 areas, and then output them to the screen?

I might have more after, but these are the ones that are bothering me right now.
Australia Forum Administrator #4
Perhaps read this and get back to us if you still have questions:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=6036

Not quite sure about getting the tables to "name themselves", but everything you want to do is certainly possible.
#5
I looked as thoroughly as I can, but nothing works to get this to work, but, as I've mentioned, my inexperience could be the reason I'm faulting.

Example :
temp1 = "%1"
temp2 = "%2"
who.place[temp1] = temp2

That works. But it overwrites all information that was on who.place.whereever.name and rewrites the latest information. I scoured over that page, and the page it links to, and couldn't find how to add in any information.

I later tried :
table.insert (who.place[temp1], temp2)

but I don't even think that can be used in such a situation. I could be mistaken, and am simply using it incorrectly.

tprint for example, on the example above, prints out :
(only using four lines rather than the full 40-ish)

"Centre Crossing"="-removed-"
"Bard's Way north of Centre Street"="-removed-"
"Fire and Spice"="-removed-"
"North of New Thera"="-removed-"

I know for certain there are four or five, (probably more), people in those specific areas, and the names that shown through were only the latest on the list.

The problem I'm having is getting it go "Centre Crossing"="-removed-,-removed-,-removed-".

Where am I going wrong with this?

-edit-

Keldar has given me a way to gather the names of the people in a specific room :


places["%2"] = places["%2"] or {}
places["%2"]["%1"] = true


and a function:


function getPeopleInRoom(room_name)
  local t = {}
  if places[room_name] then
    for p,_ in pairs(places[room_name]) do
      table.insert(t, p)
    end
    return t
  end
  return nil
end


How exactly would I be able to call this function via alias?

Example

DWHO (room_name)

I can't find any information on calling functions via aliases triggers with arguments.
Amended on Mon 01 Sep 2008 09:03 AM by Dexodro
USA #6
Check out my reply on the Achaea forums (http://forums.achaea.com/index.php?showtopic=34475) as I do something very similar.

To answer the basic question, you call it like you do anywhere else, making sure you send to 'script' or 'script (after omit)', like so:

function (argument 1, argument 2, ..)

In the case of getPeopleInRoom() above, it returns a numerically indexed table of people in the room. Here's another function you could use to print the room name and people in it using that one:


printRoom = function (room)
  local x = "" -- temporary variable x
  for i, n in ipairs (getPeopleInRoom (room)) do -- go through the names in the room
    x = x .. n -- add the name
    if i < #x then x = x .. " " end -- if it's not the end, add a space
  end -- for
  print (room .. " = " .. x)
end -- func


Which you can then call using printRoom ("room name")

edit: I found an error in the function you pasted above. Change:

for p,_ in pairs(places[room_name]) do

to

for _,p in pairs(places[room_name]) do
Amended on Mon 01 Sep 2008 09:44 AM by Fadedparadox
USA #7
I tested this whole script with a fake table, like so:


getPeopleInRoom = function (room_name)
  local t = {}
  if places[room_name] then
    for _,p in pairs(places[room_name]) do
      table.insert(t, p)
    end
    return t
  end
  return nil
end


printRoom = function (room)
  local x = "" -- temporary variable x
  for i, n in ipairs (getPeopleInRoom (room)) do -- go through the names in the room
    x = x .. n -- add the name
    if i < #x then x = x .. " " end -- if it's not the end, add a space
  end -- for
  print (room .. " = " .. x)
end -- func

places = {["Dark room."] = {"John", "Matt", "Jake"}}


And had an alias do this:

printRoom("Dark room.")


It gave me this output:

Dark room. = John Matt Jake