Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 16: | Zeile 16: | ||
end |
end |
||
− | return tonumber(string.format("%." .. (2) .. "f", gesamt)) |
+ | --return tonumber(string.format("%." .. (2) .. "f", gesamt)) |
+ | return table.tostring(gItem) |
||
+ | end |
||
+ | |||
+ | function table.val_to_str ( v ) |
||
+ | if "string" == type( v ) then |
||
+ | v = string.gsub( v, "\n", "\\n" ) |
||
+ | if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then |
||
+ | return "'" .. v .. "'" |
||
+ | end |
||
+ | return '"' .. string.gsub(v,'"', '\\"' ) .. '"' |
||
+ | else |
||
+ | return "table" == type( v ) and table.tostring( v ) or |
||
+ | tostring( v ) |
||
+ | end |
||
+ | end |
||
+ | |||
+ | function table.key_to_str ( k ) |
||
+ | if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then |
||
+ | return k |
||
+ | else |
||
+ | return "[" .. table.val_to_str( k ) .. "]" |
||
+ | end |
||
+ | end |
||
+ | |||
+ | function table.tostring( tbl ) |
||
+ | local result, done = {}, {} |
||
+ | for k, v in ipairs( tbl ) do |
||
+ | table.insert( result, table.val_to_str( v ) ) |
||
+ | done[ k ] = true |
||
+ | end |
||
+ | for k, v in pairs( tbl ) do |
||
+ | if not done[ k ] then |
||
+ | table.insert( result, |
||
+ | table.key_to_str( k ) .. "=" .. table.val_to_str( v ) ) |
||
+ | end |
||
+ | end |
||
+ | return "{" .. table.concat( result, "," ) .. "}" |
||
end |
end |
||
Version vom 3. Januar 2020, 15:18 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Goldwert/Doku erstellt werden
local p = {}
function p.main(frame)
local gProEinheit,gItem = {},{}
for key, value in string.gmatch(frame.args[1], "(%w+)_(%w+%.%w+|%w+)") do
gProEinheit[key] = value
end
for key, value in string.gmatch(frame.args[2], "(%w+)_(%w+%.%w+|%w+)") do
gItem[key] = value
end
local gesamt = 0
for k, v in pairs(gProEinheit) do
gesamt = gesamt + tonumber(v) * tonumber(gItem[k])
end
--return tonumber(string.format("%." .. (2) .. "f", gesamt))
return table.tostring(gItem)
end
function table.val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
else
return "table" == type( v ) and table.tostring( v ) or
tostring( v )
end
end
function table.key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. table.val_to_str( k ) .. "]"
end
end
function table.tostring( tbl )
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table.val_to_str( v ) )
done[ k ] = true
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result,
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
end
end
return "{" .. table.concat( result, "," ) .. "}"
end
return p