lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I wrote simple versions of a few perl std functions in Lua, since I am so
addicted to them. Here's my split:

function push( table, value )
   if type(table) == "table" then
      local n = getn( table )
      table[n+1] = value
      return 1
   end
   return nil
end

function split( delim, string )
   local endindex = 1
   local start = 1
   local pattern = "([^"..delim.."]+"..delim.."+)"
   local retval = {}
   local token = ""
   while start do
      local starti, endindexi, tokeni = strfind( string, pattern, endindex )
      if tokeni then
         start, endindex, token = starti, endindexi, tokeni
         push( retval, token )
      end
      start = starti
   end
   starti, endindexi, tokeni = strfind( string, "([^"..delim.."]+)$",
endindex )
   if tokeni then
      push( retval, tokeni )
   end
   return retval
end

> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Daniel Perry
> Sent: Monday, July 23, 2001 2:13 PM
> To: Multiple recipients of list
> Subject: split/join
>
>
> Hello,
>  I've been messing around with lua for a couple of
> days now, thought it was pretty cool, but have found
> that there aren't any split/join functions, as in
> perl. is there anything similar, or softcoded versions
> of those commands available?
>   thanks,
>  [daniel perry : mercuri labs]
>
> __________________________________________________
> Do You Yahoo!?
> Make international calls for as low as $.04/minute with Yahoo! Messenger
> http://phonecard.yahoo.com/