lua-users home
lua-l archive

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


On Tue, Dec 29, 2009 at 6:14 AM, David Manura <dm.lua@math2.org> wrote:
> I think I usually want split(table.concat(t, sep), sep) to be
> structurally equal to t, even when t contains empty strings.

Lua is unique in that all string searching/substitution functions use
patterns, not literals. So we consider it part of the learning curve
that people say '%.' if they are looking for a literal period.  But
any split() must respect the pattern's meaning. So I would expect
splitting on %s to give me a different result from splitting on %s+.
It may be least suprising if split(' one','%s+') gives {'','one'} and
would require an explicit trim() first to not include the ends, so
that split(',one',',') does give {'','one'} as required.  So I'm
convinced.

> Having sep = "" can provide an idiomatic way of converting a character
> array to and from a string (as in Perl).

Is this such a common operation? And assuming a function collect(),
which takes a single-valued sequence and returns an array, it's then
just collect(s:gmatch '.')

It's certainly a case that needs to be assigned a definite meaning.

Section 15.5.4.14 of that ECMA 262 specification referenced earlier
should be read by anyone who still thinks that split() is a
straightforward function ;)

steve d.

PS. Given that a robust well-performing trim() is also not so
straightforward, I think it's another candidate for an external core
library function.