Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Tables in Lua
It is now over 60 days since the last post. This thread is closed.
Refresh page
Pages: 1
2
Posted by
| Siluri
(5 posts) Bio
|
Date
| Reply #15 on Mon 17 Apr 2006 01:11 AM (UTC) |
Message
| Hi Nick
Thanks for the advice so far.
I note that using table.sort doesn't produce stable/repeatable results.
Any advice on how to do this please?
Furthermore I'd like to be able to do sortation on two elements of a table (using above sample data, say primary sort on name, secondary sort on class). How would you do that using table.sort? (or would you put in full code for a sort into a function and do it that way?).
Thanks. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #16 on Mon 17 Apr 2006 01:38 AM (UTC) |
Message
| How do you define stable/repeatable results? If you take a table with the same elements, the sort will always be the same, unless of course some elements are equal in which case their sort order is undefined.
To make things sort on multiple values, you could use something like this as your sort function:
function (a,b)
if a.date < b.date
return true -- primary sort
elsif a.date == b.date
return a.file < b.file -- secondary sort
else
return false -- a is >= b in this case (as far as the sort is concerned)
end
end
|
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| VincentVegas
(1 post) Bio
|
Date
| Reply #18 on Wed 29 Nov 2006 04:41 PM (UTC) |
Message
| Hello.
It might be that I am a bit dumb at the moment. :-)
I want to sort a table by its values, but that seems to be a real problem.
What I have is something like this:
age = {}
age.peter = 24
age.paul = 14
age.mary 34
I want to have this table sorted/printed according to their ages.
A table.sort(age) does nothing, nor does a table.sort(age,funcion (a,b) return age.a < age.b end)
What is the trick?
Regards
Thomas
| Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #19 on Wed 29 Nov 2006 09:12 PM (UTC) Amended on Wed 29 Nov 2006 09:14 PM (UTC) by Nick Gammon
|
Message
| The sort function only works on numerically-indexed tables, whereas your tables has keys "peter", "paul" and "mary".
You can sort it, but you must first get a numerically-keyed table. This will do it:
age = {}
age.peter = 24
age.paul = 14
age.mary = 34
-- copy keys into numeric table
t = {}
for k in pairs (age) do
table.insert (t, k) -- remember key of each item
end -- for
-- show unsorted table
table.foreach (t, print)
-- sort it
table.sort (t,
function (a, b)
return age [a] < age [b]
end -- function
)
-- show results
print "after sort..."
table.foreach (t, print)
Output
1 peter
2 mary
3 paul
after sort...
1 paul
2 peter
3 mary
What I have done here is first looped to copy the keys of your main table into a secondary (temporary) table t. You can see from the initial debugging display that they were added in the order peter, mary and paul (they are stored internally as a hash, so the order cannot really be predicted).
Now we sort the table t, using the keys to index into the age table to find which age is less than which other age.
Finally we can print the table t, which is now sorted into the correct order (paul, peter, mary).
This general technique can be used to sort anything into any sequence. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #20 on Wed 29 Nov 2006 09:17 PM (UTC) Amended on Wed 29 Nov 2006 09:21 PM (UTC) by Nick Gammon
|
Message
|
Quote:
return age.a < age.b
Semantically this is wrong anyway. This is comparing key "a" to key "b". That is, this is the same as:
return age ["a"] < age ["b"]
In other words, you are comparing the item "a" to the item "b", if they exist, which they don't in your case.
You really wanted:
return age [a] < age [b]
However my remarks about needing the numerically-indexed table still stand. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Smokebomb
(6 posts) Bio
|
Date
| Reply #21 on Fri 30 Mar 2012 06:44 PM (UTC) |
Message
| ok so i took your advice nick and fiddled around with what i already knew, learnt a bit more in the process (after 2 days work(slow learner))
Trigger: You still have to kill * * (*)
Send:
campaign = {}
mobs.mobname = {
mob = "%2", area = "%3"
}
print (campaign)
Send to: script
This is all good and well in creating a table of campaign mobs in Aardwolf but I'm really struggling to find a way to manipulate the first 2 true wildcards (the 1st is recognised as an asterisk by the 1st wildcard).
Im pretty sure my main problem is that it's sending to script and not world so i can't put something like:
Send:
campaign = {}
mobs.mobname = {
mob = "%2", area = "%3"
}
print (campaign)
table.foreach (campaign, print)
runto "%3"
where "%2"
my second problem is i dont know how to differentiate/identify ((between)) different wildcard entries on different table entries
is there a special function or command that would make the ' runto "%3" ' and then the ' where "%2" ' - send to world within the script? Can I do it using another trigger or alias or have i got completely the wrong idea? My brain is telling me that i will need to identify which is the first table entry and the string values of %2 and %3 in that entry.
I'm quite lost here as you can tell. | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #22 on Fri 30 Mar 2012 09:21 PM (UTC) |
Message
| To deal with the obvious problem:
needs to be:
Send ("runto %3")
Send ("where %2")
This because, in a script, the Send function sends its arguments to the MUD. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Smokebomb
(6 posts) Bio
|
Date
| Reply #23 on Fri 30 Mar 2012 09:54 PM (UTC) |
Message
| That's partly usefull because it does work however I need to be able to pull just the first set of %2 and %3 from the first table entry otherwise the script executes and it makes me run to every single area stored as the string for %3 for each entry (along with some additonal sends for 'hold portal' 'enter' 'down')
my trigger now looks like this :
Trigger: You still have to kill * * (*)
Send:
campaign = {}
mobs.mobname = {
mob = "%2", area = "%3"
}
print (campaign)
Send ("hold portal")
Send ("enter")
Send ("d")
Send ("hold candle")
Send ("runto %3")
Send ("where %2")
Send to: Script
So my question is - how do I pull/retrieve the first values and the first values ONLY for %2 and %3? - (because when the trigger executes next, the first entry of the table is the only one necessary for progressing, I believe the table is required to do this otherwise the script gets confused with all the different values of the wildcards. | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #24 on Sat 31 Mar 2012 07:02 AM (UTC) |
Message
| Show the whole thing please:
 |
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.
|
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Smokebomb
(6 posts) Bio
|
Date
| Reply #25 on Mon 02 Apr 2012 11:55 PM (UTC) |
Message
| <triggers>
<trigger
enabled="y"
match="Finish * off!"
sequence="100"
sound="C:\Aardwolf\MUSHclient\sounds\Decapitation Head Blood-SoundBible.com-310864499.wav"
>
</trigger>
<trigger
enabled="y"
match="Trigger: You still have to kill * * (*)"
send_to="12"
sequence="100"
>
<send>campaign = {}
mobs.mobname = {
mob = "%2", area = "%3"
}
print (campaign)
Send ("enter")
Send ("d")
Send ("runto %3")
Send ("where %2")
</send>
</trigger>
</triggers>
| Top |
|
Posted by
| Smokebomb
(6 posts) Bio
|
Date
| Reply #26 on Mon 02 Apr 2012 11:57 PM (UTC) Amended on Tue 03 Apr 2012 02:06 AM (UTC) by Nick Gammon
|
Message
| err for some reason the above post copied 2 triggers at once... this is the one in question:
<triggers>
<trigger
enabled="y"
match="Trigger: You still have to kill * * (*)"
send_to="12"
sequence="100"
>
<send>campaign = {}
mobs.mobname = {
mob = "%2", area = "%3"
}
print (campaign)
Send ("enter")
Send ("d")
Send ("runto %3")
Send ("where %2")
</send>
</trigger>
</triggers>
| Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #27 on Tue 03 Apr 2012 02:08 AM (UTC) |
Message
| Why are you setting campaign to be an empty table?
Can you show some example output, describe what is actually happening, and what you expect to happen? |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Smokebomb
(6 posts) Bio
|
Date
| Reply #28 on Tue 03 Apr 2012 02:15 PM (UTC) |
Message
| ok so working backwards I realise I had made a couple of mistakes... this is now what my trigger looks like and is working just fine but I cant seem to be able to manipulate the key entry for 'area' (%3):
<triggers>
<trigger
enabled="y"
match="You still have to kill * * (*)"
send_to="12"
sequence="100"
>
<send>campaign = {}
-- create campaign table
campaign.mobs = {
-- create sub table entries of campaign table
mob = "%2", area = "%3"
-- gives the key 'mob' the value of the second wildcard and the key?! 'area' the value of the third wildcard
}
-- close campaign table
print (campaign)
-- prints campaign table
table.foreach (campaign.mobs, print)
-- prints all the values of the subtables campaign.mobs
</send>
</trigger>
</triggers>
which gives output that looks like this (for the last 4 mobs of the campaign):
table: 0x05dedd48
area The Blighted Tundra of Andarin
mob a pine tree
table: 0x05deb020
area The Labyrinth
mob a hippogriff
table: 0x05df3770
area The Wobbly Woes of Woobleville
mob An obnoxious goat
I want to be able to runto the area of the first key entry for 'area' and locate which room the mob is in by use of a trigger sending to script.
| 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.
111,142 views.
This is page 2, subject is 2 pages long:
1
2
It is now over 60 days since the last post. This thread is closed.
Refresh page
top