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

Gammon Software Solutions forum

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

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Splitting strings at a delimiter
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Splitting strings at a delimiter
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please)
Maximum of 6000 characters. Text only please, no HTML.
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Posted by Nick Gammon   Australia  (19,434 posts)  [Biography] bio   Forum Administrator
Date Sun 28 Jul 2013 07:57 AM (UTC)  quote  ]
Message
Yes, very neat. :)

- Nick Gammon

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

Posted by Fiendish   USA  (1,004 posts)  [Biography] bio   Global Moderator
Date Sun 28 Jul 2013 03:59 AM (UTC)  quote  ]

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


http://aardwolfclientpackage.googlecode.com/
https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Curtis_D   (32 posts)  [Biography] bio
Date Sun 25 Mar 2012 12:18 PM (UTC)  quote  ]
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.
[Go to top] top

Posted by Hasz   (2 posts)  [Biography] bio
Date Sat 17 Mar 2012 05:19 PM (UTC)  quote  ]
Message
ok nice.
iam useing lua right now but have a little bit more experience in javascript.

thx jonathan
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Sat 17 Mar 2012 04:23 PM (UTC)  quote  ]

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
[Go to top] top

Posted by Hasz   (2 posts)  [Biography] bio
Date Sat 17 Mar 2012 03:26 PM (UTC)  quote  ]
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

[Go to top] top

Posted by Nick Gammon   Australia  (19,434 posts)  [Biography] bio   Forum Administrator
Date Fri 28 Apr 2006 01:31 AM (UTC)  quote  ]

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
[Go to top] top

Posted by Nick Gammon   Australia  (19,434 posts)  [Biography] bio   Forum Administrator
Date Mon 14 Nov 2005 07:28 PM (UTC)  quote  ]

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
[Go to top] top

Posted by Nick Gammon   Australia  (19,434 posts)  [Biography] bio   Forum Administrator
Date Mon 14 Nov 2005 02:57 AM (UTC)  quote  ]

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
[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.


9,506 views.

[New subject]  Start a new subject   [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.

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]