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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Parsing

Parsing

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


Posted by Terry   USA  (87 posts)  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. :)
Top

Posted by Erendir   Germany  (47 posts)  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)
Top

Posted by Terry   USA  (87 posts)  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)?
Top

Posted by Erendir   Germany  (47 posts)  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.
Top

Posted by Nick Gammon   Australia  (23,102 posts)  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
Top

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

require 'tprint'

tbl = utils.split(delimitedstring, delim)

tprint(tbl)
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.


22,051 views.

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.