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 ➜ Splitting strings at a delimiter

Splitting strings at a delimiter

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


Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Mon 14 Nov 2005 02:57 AM (UTC)

Amended on Mon 14 Nov 2005 07:33 PM (UTC) by Nick Gammon

Message
Version 3.69 of MUSHclient adds a new Lua script function: utils.split. This was suggested by Ked.

This is intended to do the reverse of table.concat. That is, it takes a string and generates a table of entries, delimited by single-character delimiters (such as comma or newline).

Example:


test = "the,quick,brown,dog,jumped"  

t = utils.split (test, ",")

tprint (t)

print (table.concat (t, ","))

Output:

1="the"
2="quick"
3="brown"
4="dog"
5="jumped"

the,quick,brown,dog,jumped


You pass utils.split 2 or 3 arguments:


  1. The string to be split

  2. The single-character delimiter

  3. (optional) the maximum number of splits to do



If the 3rd argument is not supplied, or is zero, then the entire string is split. Otherwise, it will be split the number of times you specify. eg.


t = utils.split (test, ",", 2)

tprint (t)

Output:

1="the"
2="quick"
3="brown,dog,jumped"


In this case the remaining text is placed in the 3rd table item.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 14 Nov 2005 07:28 PM (UTC)

Amended on Mon 14 Nov 2005 07:34 PM (UTC) by Nick Gammon

Message
The code for the split routine is:


static int l_split (lua_State *L) {
  const char *s = luaL_checkstring(L, 1);
  const char *sep = luaL_checkstring(L, 2);
  const int count = (int) luaL_optnumber (L, 3, 0);
  char *e;
  int i = 1;

  if (strlen (sep) != 1)
    luaL_error (L, "Separator must be a single character");

  if (count < 0)
    luaL_error (L, "Count must be positive or zero");

  lua_newtable(L);  /* result */

  /* repeat for each separator */
  while ((e = strchr(s, *sep)) != NULL && 
         (count == 0 || i <= count)) 
    {
    lua_pushlstring(L, s, e-s);  /* push substring */
    lua_rawseti(L, -2, i++);
    s = e + 1;  /* skip separator */
    }

  /* push last substring */
  lua_pushstring(L, s);
  lua_rawseti(L, -2, i);

  return 1;  /* return the table */
}

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #2 on Fri 28 Apr 2006 01:31 AM (UTC)

Amended on Fri 28 Apr 2006 02:03 AM (UTC) by Nick Gammon

Message
The equivalent code in Lua is:


function split (s, delim)

  assert (type (delim) == "string" and string.len (delim) > 0,
          "bad delimiter")

  local start = 1
  local t = {}  -- results table

  -- find each instance of a string followed by the delimiter

  while true do
    local pos = string.find (s, delim, start, true) -- plain find

    if not pos then
      break
    end

    table.insert (t, string.sub (s, start, pos - 1))
    start = pos + string.len (delim)
  end -- while

  -- insert final one (after last delimiter)

  table.insert (t, string.sub (s, start))

  return t
 
end -- function split



This version does not incorporate the "count" argument, although you could add it easily enough. However unlike the version in the utils library, it will handle a multi-character delimiter.

- Nick Gammon

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

Posted by Hasz   (2 posts)  Bio
Date Reply #3 on Sat 17 Mar 2012 03:26 PM (UTC)
Message
hola mushClient people,

can i split strings without a seperator?

like
var theString = "abcde"
var myArray

to

myArray = split.theString

myArray[0] = "a"
myArray[1] = "b"
myArray[2] = "c"
myArray[3] = "d"
myArray[4] = "e"

???

is there a easy way? or do i have to write a new function which does that?

thanks

Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #4 on Sat 17 Mar 2012 04:23 PM (UTC)

Amended on Sat 17 Mar 2012 04:24 PM (UTC) by Twisol

Message
That doesn't look like Lua, Hasz. Are you using JScript? If so, you can just use var t = "foo".split('').

In the case of Lua, just do this:
local s = "foo"

local t = {}
for i=1,#s do
  t[i] = s:sub(i,i)
end

'Soludra' on Achaea

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

Posted by Hasz   (2 posts)  Bio
Date Reply #5 on Sat 17 Mar 2012 05:19 PM (UTC)
Message
ok nice.
iam useing lua right now but have a little bit more experience in javascript.

thx jonathan
Top

Posted by Ashleykitsune   (33 posts)  Bio
Date Reply #6 on Sun 25 Mar 2012 12:18 PM (UTC)
Message
Nick Gammon said:

The equivalent code in Lua is:

-----
function split (s, delim)

  assert (type (delim) == "string" and string.len (delim) > 0,
          "bad delimiter")

  local start = 1
  local t = {}  -- results table

  -- find each instance of a string followed by the delimiter

  while true do
    local pos = string.find (s, delim, start, true) -- plain find

    if not pos then
      break
    end

    table.insert (t, string.sub (s, start, pos - 1))
    start = pos + string.len (delim)
  end -- while

  -- insert final one (after last delimiter)

  table.insert (t, string.sub (s, start))

  return t
 
end -- function split
-----

This version does not incorporate the "count" argument, although you could add it easily enough. However unlike the version in the utils library, it will handle a multi-character delimiter.



I could probably use this to create tables for my lines that are delimited by "|" characters, I'm going to give it a try.
Top

Posted by Fiendish   USA  (2,535 posts)  Bio   Global Moderator
Date Reply #7 on Sun 28 Jul 2013 03:59 AM (UTC)

Amended on Sun 28 Jul 2013 04:14 AM (UTC) by Fiendish

Message
I think a neater string:split function in Lua is (leaving in the ability to use actual patterns instead of just plain strings):


function string:split(pat)
   local fields = {}
   local start = 1
   self:gsub("()("..pat..")", 
      function(c,d)
         table.insert(fields,self:sub(start,c-1))
         start = c + #d
      end
   )
   table.insert(fields, self:sub(start))
   return fields
end


https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #8 on Sun 28 Jul 2013 07:57 AM (UTC)
Message
Yes, very neat. :)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


39,260 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.