lua-users home
lua-l archive

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


MobileAgent.lua = <<
array1 = { "10.10.2.3", "10.10.4.3" }

array2 = { ip1="10.2.3.4", , ip2="10.3.2.4" }

function update_array1 (t)
--  (some code )
end

function update_array2 (t)
--  (some code )
end
>>

Test.lua = <<
local filename = "MobileAgent.lua"
local f = assert(io.open(filename, "rb"))
local file = assert(f:read("*a"))
f:close()

local ip = "255.255.255.0"
local newFile = string.gsub(file, 'array1 = {(.-) }', 'array1 = {%1, "' .. ip .. '" }')
newFile = string.gsub(newFile, 'array2 = {(.-)ip(%d+)="([^"]+)" }',
		function (body, ipNb, lastIp)
return 'array2 = {' .. body .. ', ip' .. ipNb .. '="' .. lastIp .. '", ip' .. (ipNb+1) .. '="' .. ip .. '" }'
		end)

local f = assert(io.open(filename, "wb"))
assert(f:write(newFile))
f:close()
>>

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --