[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: split method?
- From: Dave Collins <Dave.Collins@...>
- Date: Tue, 28 Jun 2011 10:41:13 -0400
Looks like my split technique doesn’t work on "."
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
local foo = "foo-bar"
dump_table(foo:split("-"))
1=foo
2=bar
local foo = "foo~bar"
dump_table(foo:split("~"))
1=foo
2=bar
local foo = "foo.bar"
dump_table(foo:split("."))
1=
2=
3=
4=
5=
6=
7=
8=
Sigh. Back to the drawing board...
Dave Collins
Front-End Engineer
Mercatus Technologies Inc.
60 Adelaide Street East, Suite 700
Toronto ON M5C 3E4
T 416 603 3406 x 298
F 416 603 1790
dave.collins@mercatustechnologies.com
www.mercatustechnologies.com
-----Original Message-----
From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of Norbert Kiesel
Sent: Monday, June 27, 2011 2:06 PM
To: lua-l@lists.lua.org
Subject: Re: split method?
On Mon, 2011-06-27 at 12:37 -0400, Dave Collins wrote:
> I’ve used this split (string to table) method I found here:
> http://stackoverflow.com/questions/1426954/split-string-in-lua.
>
>
>
> I’ve got it working, but I don’t understand it.
Read the manual again, especially the part about Function calls (2.5.8).
>
>
>
> function string:split(delimiter)
>
> local result = { }
>
> local from = 1
>
> local delim_from, delim_to = string.find( self, delimiter, from
> )
>
> while delim_from do
>
> table.insert( result, string.sub( self, from ,
> delim_from-1 ) )
>
> from = delim_to + 1
>
> delim_from, delim_to = string.find( self, delimiter, from
> )
>
> end
>
> table.insert( result, string.sub( self, from ) )
>
> return result
>
> end
>
>
>
> This is how I’m calling it:
>
>
>
> local myDateString = "2011-06-21"
>
> local myDateTbl = myDateString.split(myDateString,"-")
You could (should?) call it like that:
local myDateTbl = myDateString:split("-")
Function definitions using : add an implicit first parameter called
"self". Again, read section 2.5.8 of the manual (or the relevant
sections from the wonderful Lua Wiki).
</nk>