Jump to content

User:DePiep/help/luadoc

From Wikipedia, the free encyclopedia
USER: /home-E /subpg · /sbox-E · /1 2 3 4 5 6  · {{X32}} (E) /help* /news /s-E /x-E /xx-E /row-E  EXTERNAL: EL:w3c-css · TOOLS: WD: rgx101 PETSCAN  · wtech /tpu · commons
WIKI: special:ExpandT Prefix (T:list) #parse {{MAGIC}} H:notes · _TOC_ WikiBlame mw:Lua man (patterns, debug, α) · T:SFW/s (M:,../sandbox· M:String · TFA /better signpost adnws · SPEEDY CFSD · alerts
/unicode: {{T:U+}} m:Uchar · U+abbr · {{ISO15924}} ff conv · {{ISO4217}} BOXES: /chembox /ChemData  · /drugbox T · TG · DEV: /CUR · /taxo · /chemlist · NBSP> < minus>−< NDASH>–< MDASH>—< NOT EQUAL>≠< DOTCIRCLE>◌<
ELEM: pv
/elem · WT · ISO-T symbol-to-data · datarefs · :c:cat:PT · U:PTG · WP:PTG (t)
This box: view · edit



wd
Wikibase Lua

general

[edit]
exemplary

decode, encode &nbsp;

[edit]
Module:decodeEncode decode, encode

table

[edit]

3 x 4 table

[edit]
local grid = {
	{'a1', 'a2', 'a3'},
	{'b1', 'b2', 'b3'},
	{'c1', 'c2', 'c3'},
	{'d1', 'd2', 'd3'}
}
To read the "b3" value, and then write a new value to it, you would do this:
local x = grid[2][3]
mw.log(x) -- gives "b3"
grid[2][3] = 'foo'
x = grid[2][3]
mw.log(x) -- gives "foo"

Mr. Stradivarius 13:29, 11 May 2014 (UTC)

string

[edit]

ucfirst

[edit]
mw.ustring.upper(mw.ustring.sub(str, 1, 1)) .. mw.ustring.sub(str, 2)

Jackmcbarn 20:36, 13 April 2014 (UTC)

You can also use mw.language.getContentLanguage():ucfirst(str). — Mr. Stradivarius 12:30, 22 April 2014 (UTC)
Or mw.ustring.gsub("^.", mw.ustring.upper). — Eru·tuon 01:33, 8 July 2018 (UTC)

Wikipedia_talk:Lua How_to_use_a_string_read_as_tablename?

[edit]
Wikipedia talk:Lua § How to use a string read as tablename?
06:08, 23 April 2022 (UTC)

I have read a variable as string, say 'tFoo'. Now I want that name to be used as a table id: table.insert( 'tFoo', 'someValue1' ). This produces an error (like "error input variable 1: table expected, got string"). How can I use that string as table-name in there? (note: the table name is known beforehand, from a limited list of five, so is declared as local tFoo = {}). In code, essense lines:

local someFunction()
local tFoo = {}
local sInput
   sInput = 'tFoo'
   table.insert( 'tFoo', 'someValue1' ) -- triggers error
   return table.concat( 'tFoo', ";" )
end

DePiep (talk) 06:20, 22 April 2022 (UTC)

local function someFunction()
	local tFoo = {}
	local sInput
	sInput = 'tFoo'
	table.insert( tFoo, 'someValue1' )
	return table.concat( tFoo, ";" )
end
Johnuniq 07:23, 22 April 2022 (UTC)
Hmm, I just read this again. Are you saying you want the name of the table to be determined when the program runs? You can't do that in Scribunto's Lua. You would need to have a series of if...elseif...end tests to work out which table to use. If you describe the problem a bit more I can provide an example. Johnuniq 07:26, 22 April 2022 (UTC)

As Johnuniq said, but to avoid if...elseif...end tests use a table of all five tables like in

function p.test()
   local tfoo1 = {1,2,3}
   local tfoo2 = {4,5,6}

   local all_tables = {}
   all_tables["tFoo1"] = tfoo1
   all_tables["tFoo2"] = tfoo2

   local sInput = 'tFoo2'
   local operate_on = all_tables[sInput] or {}
   
   table.insert(operate_on , 'someValue1' )

   return table.concat(operate_on, "; " )
end

This returns "4; 5; 6; someValue1". I introduced "operate_on" for the case your sInput does not match any of the tables, but you probably do not need it. User:Ponor 08:41, 22 April 2022 (UTC)


args and metatables

[edit]

Johnuniq on how frame is not empty: [1]


Mr Strad detailing metatable:

local p = {}
local data1 = ''
local e2id

function eToId(e)
	return e	
end

function p.element(frame)
	local args = frame.args  -- arguments in #invoke
	local first = args[1] or 'no value for first'  -- first unnamed argument
	local dummy = args['dummy'] or 'no value for dummy'
	
	local eId = eToId(e)
	
	local e = {}
	
	-- Make a copy of the args table. The values will actually be present in
	-- argsCopy, so the # operator will work on it. The # operator doesn't work
	-- on the args table directly, because it doesn't actually contain any
	-- values. (The values are found through the use of metatables.)
	local argsCopy = {}
	for k, v in pairs(args) do
		argsCopy[k] = v
	end
	
	table.insert(e, '')
	return '*Value of #args (I don\'t think this does anything useful): ' .. #args ..
		'\n*Value of #argsCopy: ' .. #argsCopy ..
		'\n*First unnamed argument: ' .. first ..
		'\n*Dummy argument: ' .. dummy
end

function p.metatable()
	-- This function shows how metatables work with the # operator.
	-- The arguments table (frame.args) uses something similar to this, so
	-- this example should explain why # doesn't work on it. 
	local ret = ''

	-- At first, t1 is just a blank table.
	local t1 = {}
	ret = ret .. 't1 with no metatable:'
	ret = ret .. '\n* First unnamed argument: ' .. (t1[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t1
	
	-- t2 contains the actual values we want.
	local t2 = {'the first t2 value', 'the second t2 value'}
	ret = ret .. '\n\n'
	ret = ret .. 't2, which holds the arguments:'
	ret = ret .. '\n* First unnamed argument: ' .. (t2[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t2
	
	-- Now we attatch a metatable to t1. We haven't given any instructions to
	-- the metatable yet, so it won't do anything for now.
	local metatable = {}
	setmetatable(t1, metatable)
	ret = ret .. '\n\n'
	ret = ret .. 't1 with a blank metatable:'
	ret = ret .. '\n* First unnamed argument: ' .. (t1[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t1
	
	-- And now we add an __index metamethod to the metatable. This example
	-- tells Lua to look up values in t2 if the value in t1 is nil.
	-- t1 still doesn't contain any actual values, so its length is still
	-- reported as 0.
	metatable.__index = t2
	ret = ret .. '\n\n'
	ret = ret .. 't1 with an __index metamethod set to t2:'
	ret = ret .. '\n* First unnamed argument: ' .. (t1[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t1

	-- Now we write a new value to t1. This value is actually present in t1, so
	-- Lua doesn't look up the value in t2, and it is picked up by the #
	-- operator.
	t1[1] = 'A new first value for t1'
	ret = ret .. '\n\n'
	ret = ret .. 't1 with an __index metamethod set to t2, and a value written to the first field:'
	ret = ret .. '\n* First unnamed argument: ' .. (t1[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t1
	
	return ret
end

return p

Johnuniq on frame & arg

Yes, that's easy.

local pframe = frame:getParent()
local config = frame.args  -- arguments from the template definition
local args = pframe.args   -- arguments from the page calling the template

If a page contains {{example|abc|def}} and Template:Example contains {{#invoke:example|main|hello}}, and after the above code, config[1] is 'hello' and args[1] is 'abc' while args[2] is 'def'. Johnuniq (talk) 06:27, 7 August 2014 (UTC)

|}