Register forum user name Search FAQ

Gammon Forum

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 ➜ String Manipulation

String Manipulation

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


Pages: 1  2  3 4  

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #30 on Fri 10 Feb 2012 01:27 AM (UTC)

Amended on Sat 11 Feb 2012 02:11 AM (UTC) by Kevnuke

Message

local JSON = require"json"
require "tprint"


content = [=[
[
  { "name": "Vision", "rank": "Adept" },
  { "name": "Avoidance", "rank": "Capable" },
  { "name": "Tattoos", "rank": "Inept" },
  { "name": "Survival", "rank": "Transcendent" },
  { "name": "Weaponry", "rank": "Adept" },
  { "name": "Riding", "rank": "Inept" },
  { "name": "Venom", "rank": "Transcendent" },
  { "name": "Hypnosis", "rank": "Mythical" },
  { "name": "Subterfuge", "rank": "Transcendent" },
  { "name": "Constitution", "rank": "Inept" },
  { "name": "Thermology", "rank": "Inept" },
  { "name": "Frost", "rank": "Inept" },
  { "name": "Antidotes", "rank": "Inept" },
  { "name": "Fitness", "rank": "Apprentice" },
  { "name": "Galvanism", "rank": "Inept" },
  { "name": "Philosophy", "rank": "Inept" },
  { "name": "InkMilling", "rank": "Inept" },
  { "name": "Gathering", "rank": "Inept" }
]
]=]

local t = {}
system["char"] = "Setru"

t  =  (JSON.decode("[" .. content .. "]")[1])

for i = 1, #t in ipairs (t) do

  system[system.char]  =  {

    skills  =  {

      [t[i].name]  =  {

        rank      =  t[i].rank,

        abilities  =  {

        },

      },

    },
	
  }

end

tprint (system[system.char].skills)

end


I keep getting a compile error
Compile error
World: Achaea
Immediate execution
[string "Immediate"]:11: 'do' expected near 'in'

I'm trying to end up with this..


system["Setru"].skills = {
  Vision  =  {
    rank = "Adept"
  },
  Avoidance  =  {
    rank = "Capable"
  },
  Tattoos  =  {
    rank = "Inept"
  },
  Survival  =  {
    rank = "Transcendent"
  },
  Weaponry  =  {
    rank = "Adept"
  },
  Riding  =  {
    rank = "Inept"
  },
  Venom  =  {
    rank = "Transcendent"
  },
  Hypnosis  =  {
    rank = "Mythical"
  },
  Subterfuge  =  {
    rank = "Transcendent"
  },
  Constitution  =  {
    rank = "Inept"
  },
  Thermology  =  {
    rank = "Inept"
  },
  Frost  =  {
    rank = "Inept"
  },
  Antidotes  =  {
    rank = "Inept"
  },
  Fitness  =  {
    rank = "Apprentice"
  },
  Galvanism  =  {
    rank = "Inept"
  },
  Philosophy  =  {
    rank = "Inept"
  },
  InkMilling  =  {
    rank = "Inept"
  },
  Gathering  =  {
    rank = "Inept"
  }
}


EDIT: Made the "content" string easier to read and entered a missing JSON function. Used the MUSHclient forum codes converter on this post. :P
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #31 on Fri 10 Feb 2012 02:17 AM (UTC)

Amended on Fri 10 Feb 2012 02:19 AM (UTC) by Twisol

Message
Well, ignoring the compilation error for a moment (because I haven't found it yet), your decoding isn't doing what you want. You have something that's basically "[{'a':2},{'b':3}]", and you're decoding it and getting the first element. And then iterating over it. But that's just {a: 2}.

Basically, it looks like you dropped the "[" .. content .. "]" from the decode line. That should always be present, because you're insuring against the possibility that the incoming payload isn't an object or array. The [1] serves to unwrap the payload from this extraneous array after it's been decoded.

As for the compile error, it's because you mixed up the numeric and generic for-loops. Numeric-for is "for i=start, end, step do"; generic-for is "for i, v in ipairs(t) do". You have an odd "for i=start, end in ipairs(t) do" sort of construct, which makes no sense to Lua.

[EDIT]: You should probably also use MUSHclient's "Edit -> Convert Clipboard Forum Codes" menu item to escape your [i]'s and such. Otherwise your post will end up italicized and with broken code, like what just happened.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #32 on Sat 11 Feb 2012 02:17 AM (UTC)
Message
Ok so I made those changes to the post using the MUSHclient Clipboard forum codes editor.

I'm a little clueless about using for loops in my code. The ones I have are ones Trevize showed me how to do and I changed them slightly as needed. I don't even know how what I'm trying to do would look.

Any code examples or help would be appreciated. Thanks!
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #33 on Sat 11 Feb 2012 03:28 AM (UTC)
Message
I think I got it!


for k,v in pairs (t) do

  system[system.char]  =  {

    skills  =  {

      [v.name]  =  {

        rank  =  v.rank,

        abilities  =  {

        },

      },

    },
	
  }

end
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #34 on Sat 11 Feb 2012 04:12 AM (UTC)

Amended on Sat 11 Feb 2012 04:17 AM (UTC) by Twisol

Message
Only change I would make at this point, is using ipairs() instead of pairs(). ipairs() goes over only integer indices (so it's for array-like tables), but guarantees to go over them in order. On the other hand, pairs() goes over every key in the table, but in an officially undefined manner.

Kevnuke said:
I'm a little clueless about using for loops in my code. The ones I have are ones Trevize showed me how to do and I changed them slightly as needed. I don't even know how what I'm trying to do would look.

Generic-for is traditionally harder to grasp than numeric-for for most learners of Lua. Essentially, in the line for k,v in pairs(t) you have two parts: the "iterator" on the right, and the loop variables on the left. Every iterator supplies its own set of loop variables. You could create your own iterator if you wanted; I've written a linked-list iterator in the past that was used like this:
for item, prev in links(t) do
  -- item is the current item
  -- prev is the previous item, in case you want to remove the current one.
end

Or an iterator could be written to loop forever, perhaps going over every item in an array and wrapping around at the end.
for i, v in circle(t) do
  -- around and around
  -- use 'break' to finish if necessary
end


Generic-for is called generic precisely because you can loop differently based on your needs.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #35 on Mon 13 Feb 2012 12:22 AM (UTC)
Message
I got the same result with pairs and ipairs. Is ipairs faster since it doesn't check non-numerical indices?
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #36 on Mon 13 Feb 2012 01:16 AM (UTC)
Message
They might come out to the same thing in this particular case, but ipairs() better expresses that you're iterating over a list, rather than arbitrary key/value pairs.

I don't think either one is faster than the other. They're just for different things.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #37 on Mon 13 Feb 2012 07:18 AM (UTC)
Message
If there is a difference in speed it's probably negligible. You'd have to run the same thing through both functions a few million times to see the difference.

Should I even worry about things like that when writing my scripts? Like how much clock time a script will use?
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #38 on Mon 13 Feb 2012 08:32 AM (UTC)
Message
My mantra is to make it work first, then make it beautiful, and only after that should I make it fast. You might like to do the same. :)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #39 on Tue 14 Feb 2012 12:44 AM (UTC)
Message
Kevnuke said:

Should I even worry about things like that when writing my scripts? Like how much clock time a script will use?


No, don't worry about that. Make it readable.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #40 on Fri 17 Feb 2012 03:40 AM (UTC)

Amended on Fri 17 Feb 2012 03:47 AM (UTC) by Kevnuke

Message
Thanks for the help guys. That takes a load off of my mind. I should be okay with that now that I have a grasp on how to do that part. I believe I said before that I was going to make a table of all the rooms I come across in achaea using the room's ID number as the index and information about the room such as it's visible name and items in that room as a sub-table.


--see if this room is already in the map table and if not create an index for it containing an empty table
if not system.map[12346] then
  system.map[12346] = {}
end

system.map[12345] = {
  name         =  "Centre Crossing",
  description  =  "blah blah blah the center of Cyrene",
  exits        =  {
    n            =  system.map[12346],
    s            =  system.map[12347],
    e            =  system.map[12348],
    w            =  system.map[12349],
    in           =  system.map[12356],
    [dash east]  =  system.map[12389],
  },
  items  =  {
    system.items[874787],
    system.items[455729],
  }
}


I was going to make a for loop to check for the existence of an index in the system.map table for each room ID.

I'm a little confused as to the syntax to use on this though. Iterate through the named indices of the "exits" table resulting from using json.decode on the Room.Info message from GMCP.

like this..


t = json.decode("[" .. content .. "]")

-->

t[exits] = {
  n = 12346,
  s = 12347,
  e = 12348,
  w = 12349
}

Not sure how to make it check for each room in the exits table.

EDIT: I just had an idea. Is this it?


for k, v in pairs (t.exits) do
  if not system.map[v] then
    system.map[v] = {}
  end
end
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #41 on Fri 17 Feb 2012 04:03 AM (UTC)

Amended on Fri 17 Feb 2012 04:04 AM (UTC) by Twisol

Message
Kevnuke said:
EDIT: I just had an idea. Is this it?


for k, v in pairs (t.exits) do
  if not system.map[v] then
    system.map[v] = {}
  end
end

Yep, that's it. Good work!

[EDIT]: You're using things like [exits] and [dash east], though, where I assume you meant ["exits"] and ["dash east"]. Careful there.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #42 on Fri 17 Feb 2012 06:33 PM (UTC)
Message
Twisol said:

You're using things like [exits] and [dash east], though, where I assume you meant ["exits"] and ["dash east"]. Careful there.


Yea something I need to be mindful of. Thanks for catching that!
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #43 on Fri 17 Feb 2012 09:09 PM (UTC)
Message
This is going to have to keep track of the room I was just in as well. unless IRE decides to add an entry in the exits object for the room ID of the room you end up in if you dash in that direction, or use non-standard movement like worm warp, wings, and the parthren gare. In the case of worm warp it would have to remove the room i'm in as the destination room of -that- room's worm warp exit too.

I'm wondering what kind of administrative distance value I'm going to assign for each type of movement. The value will be different depending on if that type of movement puts me at my destination or not. Worm warp has a few seconds of balance recovery, but if using it puts me in the destination room then it doesn't matter because there won't be any movement after that anyway.

I suppose if you have the dash ability, enough endurance, and a low enough ping it will always be faster than any other way of getting from one room to another on the same plane. I've already considered that certain routes might need a standard movement to "back up" a room or two back the way you came from a dash to put you into position to dash again. Unless the script that generates the route finds a faster way to get to that place that doesn't require taking that path.

Any other things I should consider for this endeavor?
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #44 on Sun 15 Apr 2012 08:51 PM (UTC)

Amended on Mon 16 Apr 2012 07:25 AM (UTC) by Nick Gammon

Message
Had a quick question about your SendGMCP function Twisol. Does it handle GMCP messages with empty string payloads, like Char.Items.Inv or number payloads like Char.Items.Contents 128976?

I was going to use such messages in an alias for INFO RIFT and INFO INV and was thinking I might be better off just putting the relevant Telnet codes and GMCP message directly into SendPkt, like so..



	<alias
		match="^INFOINV$"
		enabled="y"
		regexp="y"
		ignore_case="y"
		sequence="100"
		send_to="12"
		script="ProvokeInventory"
	/>

function ProvokeInventory()

	SendPkt("\255\250\201Char.Items.Inv\255\240")

end

	<alias
		match="^INFOCONT (\d+)$"
		enabled="y"
		regexp="y"
		ignore_case="y"
		sequence="100"
		send_to="12"
		script="ProvokeContainer"
	/>

function ProvokeContainer (name, output, wildcs)

	SendGMCP ("Char.Items.Contents ", wildcs[1])

end



I'll have checks in there once I get everything setup correctly to make sure that the number I use is actually in my inventory or in the room I'm in and make sure it's a valid argument. Number not a string, for example. But does that at least work?
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.


115,140 views.

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

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

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.