--- -- Interpolates values into a string. -- -- Values are encoded in the form %(key,format) -- where key is the index into the values table -- and format is the string.format option without -- the % character. -- -- @param s the string into which values are inserted -- @param tab the table of values for lookup -- @return function interp(s, tab) return (s:gsub('%%%((%a[^,]*),([-0-9%.]*[cdeEfgGiouxXsq])%)', function(k, fmt) local f = loadstring("return " .. k) setfenv(f, tab) value = f() return value and ("%"..fmt):format(value) or '%('..k..')'..fmt end)) end -- Sets interpolation for the '%' operator on strings getmetatable("").__mod = interp -- Example t = {key = "concentration", val = {56.2795, 1.2}} print( "%(key,s) is %(val[2],7.2f)%" % t )
You need to create an account or log in to post comments to this site.