lua-users home
lua-l archive

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


Adam D. Moss wrote:
> Mike Pall wrote:
> >it seems to be a common idiom to use string.gsub in combination
> >with a substitution table.
> Is it though?  What's this commonly used for?

Romulo Bahiense already listed many common uses. I think it's
even mentioned in PIL (if not then it should be).

Just from the top of my head:

I've used this idiom many times in text processing: e.g. DynASM
(the preprocessing assembler for LuaJIT) has 3 instances of this
(and it's not that big). It can be used for replacing custom HTML
tags, Wiki names and so on. The DNA benchmarks from the shootout
use this idiom extensively.

I guess 'replace $key in a string with fields from a record'
(mentioned by Romulo) is the most common variant (that's why
e.g. Python has a special syntax for that).

It's often used in conjunction with a cache table and this could
be written more elegantly by passing a table with an __index