[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Parsing
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Parsing

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


Posted by Terry   USA  (87 posts)  [Biography] bio
Date Sat 01 Aug 2009 04:09 AM (UTC)

Amended on Sat 01 Aug 2009 04:20 AM (UTC) by Terry

Message
What would be the best way of parsing a list? If there's no function, here's an algorithm I've written. My problem is that I don't know how to do this in Lua.

Is the delim not in the string?
  True:
    return the string as a table
  False:
    size = the number of times the delim is found in the string + 1
    allocate a table to `size' elements
    While there is a delim in the string:
      store all the characters before the first delim in the next open element
      remove everything up to and including the first delim in the string
    return the table


Thanks for the help. :)
[Go to top] top

Posted by Erendir   Germany  (47 posts)  [Biography] bio
Date Reply #1 on Sat 01 Aug 2009 04:23 AM (UTC)
Message

local teststr = "1st,2nd,3rd,"
local delim = ','
local t = {}
for l in string.gmatch(teststr, "[^"..delim.."]+") do
 t[#t+1] = l
end

the above code works only with 1-character long delim. Also, in case "a,,b" it just skips the 2nd ',' (there can be cases, that You wants to have an extra EMPTY or nil entry)
[Go to top] top

Posted by Terry   USA  (87 posts)  [Biography] bio
Date Reply #2 on Sat 01 Aug 2009 04:31 AM (UTC)

Amended on Sat 01 Aug 2009 04:38 AM (UTC) by Terry

Message
Thanks :) By the way, how could you then print the list so it looks like "1st, 2nd, and 3rd"? Or, for example, if t only had two elements, it show "1st and 2nd".

Edit: The issue is not knowing how many elements are in t beforehand.

Oh, and also, how would I go about deleting the first element after I'm finished with it? Would I just use table.remove(t,1)?
[Go to top] top

Posted by Erendir   Germany  (47 posts)  [Biography] bio
Date Reply #3 on Sat 01 Aug 2009 04:46 AM (UTC)

Amended on Sat 01 Aug 2009 05:05 AM (UTC) by Erendir

Message

for i = 1, #t-2 do
	Tell(t[i]..delim..' ')
end
	Tell(t[#t-1]..' and '..t[#t]..'\n')

Tell is like io.write in Lua.
Edit:
You can just try it :)
And/or read the manual:
Quote:

table.remove (table [, pos])

Removes from table the element at position pos, shifting down other elements to close the space, if necessary. Returns the value of the removed element. The default value for pos is n, where n is the length of the table, so that a call table.remove(t) removes the last element of table t.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sat 01 Aug 2009 05:55 AM (UTC)
Message
This may or may not be what you want to parse a delimited list:

http://www.gammon.com.au/scripts/doc.php?lua=utils.split

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by WillFa   USA  (525 posts)  [Biography] bio
Date Reply #5 on Sat 01 Aug 2009 05:57 AM (UTC)
Message

require 'tprint'

tbl = utils.split(delimitedstring, delim)

tprint(tbl)
[Go to top] 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.


5,962 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]