Geen bewerkingssamenvatting |
Geen bewerkingssamenvatting |
||
| Regel 22: | Regel 22: | ||
-- fuzzysep (need a better name) lets you use a series of single characters (OR not AND) | -- fuzzysep (need a better name) lets you use a series of single characters (OR not AND) | ||
local fuzzy = frame.args.fuzzysep or mw.text.trim(frame.args[6]) or "false" | local fuzzy = frame.args.fuzzysep or mw.text.trim(frame.args[6]) or "false" | ||
--[[ | |||
if fuzzy == "true" then | if fuzzy == "true" then | ||
local sepPattern = '[^' .. sep .. ')+' | local sepPattern = '[^' .. sep .. ')+' | ||
| Regel 34: | Regel 35: | ||
return newVals | return newVals | ||
end | end | ||
--]] | |||
--Standard approach | --Standard approach | ||
Versie van 5 sep 2023 19:47
Module:Arraymap
Summary
Module similar in functionality to PageForms's arraymap.
==Simple==
{{#invoke:Arraymap |map
|vals=Álvar,Núñez ,, Cabeza,de Vaca,,|sep=,,
|varstr=xxx |map=(xxx) |outputsep=;\s
}}
Luafout in mw.text.lua op regel 25: bad argument #1 to 'match' (string expected, got nil)
Or, using unnamed, sequential approach:
{{#invoke:Arraymap |map |Álvar,Núñez ,, Cabeza, de Vaca,, |, |xxx |[xxx] |;\s}}
Luafout in mw.text.lua op regel 25: bad argument #1 to 'match' (string expected, got nil)
Special use cases
If sep needs to be a space, use %s?
Escaped syntax for curly braces and pipes
Escape {{... }} as %{%{ ... %}%}
Escape | as %
{{#invoke:Arraymap |map
|vals=Persoon/-1002367743,Persoon/400
|sep=,
|varstr=xxx
|map=%{%{#ask: [[xxx]] %mainlabel=- %?Has name= %}%}
|outputsep=;\s
|escaped=true
|fuzzy=false
}}
Luafout in mw.text.lua op regel 25: bad argument #1 to 'match' (string expected, got nil)--[[
{{#invoke:Arraymap
|vals=
|sep=
|varstr=
|map=
|outputsep=
}}
]]--
local p = {}
p.map = function(frame)
local vals = frame.args.vals or mw.text.trim(frame.args[1]) or ""
local sep = frame.args.sep or mw.text.trim(frame.args[2]) or ","
local varstr = frame.args.varstr or mw.text.trim(frame.args[3]) or "@@@"
local map = frame.args.map or mw.text.trim(frame.args[4]) or ""
local outputsep = frame.args.outputsep or mw.text.trim(frame.args[5]) or ""
local outputsep = string.gsub( outputsep, [[\s]], " " ) --allow for spaces with \s
-- fuzzysep (need a better name) lets you use a series of single characters (OR not AND)
local fuzzy = frame.args.fuzzysep or mw.text.trim(frame.args[6]) or "false"
--[[
if fuzzy == "true" then
local sepPattern = '[^' .. sep .. ')+'
local valTable = {}
for val in string.gmatch( vals, sepPattern ) do
-- replace varstr in map with val
local val = mw.text.trim( val )
local newVal = string.gsub( map, varstr, val )
table.insert( valTable, newVal )
end
local newVals = table.concat( valTable, outputsep )
return newVals
end
--]]
--Standard approach
local valTable = {}
local valTable = mw.text.split( vals, sep )
local mappedTable = {}
for k,v in pairs(valTable) do
local v = mw.text.trim( v )
local newVal = string.gsub( map, varstr, v )
table.insert( mappedTable, newVal )
end
local newVals = table.concat( mappedTable, outputsep )
return newVals
end
return p
