lua-users home
lua-l archive

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


On Mon, Jan 18, 2010 at 12:41 AM, Vaughan McAlley wrote:
> 2010/1/18 Majic:
>> WHAT IS THIS G?!
> Global, I think (not to be confused with global variables :-) )

I've asked that too.  Maybe it's something to add to the manual.

To quote the Lhf, "The name gsub was taken from AWK, way back in Lua
2.5."  [1]  You also see a variant of it (/g) make its way into Perl
[2].

> always tripped me up in the differences between string.sub() and
> string.gsub()  I keep trying thinking about how string.match() and
> string.gmatch() work, and then I fumble when I remember that
> string.sub() has nothing to do with patterns.

"sub" is the prefix of many words, a handful of which are common in
Lua programs: substring, substitute, subroutine, subtract, ....
Furthermore, mathematical indexing (a_b) is read "a sub b".  A
substring is in a way a generalization of indexing a string, so in two
ways string.sub makes some sense.  string.gsub could perhaps be
renamed string.gsubst or, since no analogous string.subst exists, just
call it string.subst.  I think most programmers would immediately
guess what string.subst is, except maybe the part where it takes a
pattern and function rather than plain strings.

[1] http://lua-users.org/wiki/LuaReferenceManualComments
[2] http://www.perl.com/doc/manual/html/pod/perlop.html#Regexp_Quote_Like_Operators