Lua change string using a loop

45 views Asked by At

Hi there guys how can I make a string change its value randomly or based on a list of names?

The script I have only changes from one string to another one. Here's my code:

local string_to_find = 'o(*_*)o'
local string_to_write = '^1o(*_*)o'

ms = createMemScan()
ms.firstScan(soExactValue, vtString, 0, string_to_find, "" , 0, 0xffffffffffffffff, "", fsmNotAligned, "1", false, false, false, false)
ms.waitTillDone()

f=createFoundList(ms);
f.initialize();

resultToWrite = stringToByteTable(string_to_write .. string.char(0))

for i = 0, f.Count - 1 do
writeString(f.Address[i], string_to_write)
writeBytes(("0x" .. f.Address[i]) + 4, 0)
end

f.destroy()
ms.destroy()

Thanks in advance!

I was trying to change a string.

1

There are 1 answers

3
darkfrei On

Making a string change its value randomly, the code:

function randomNumbers (str)
    local matches = {}
    for match in str:gmatch("#") do
        table.insert(matches, match)
    end

    for i = #matches, 1, -1 do
        local n = tostring(math.random(0, 9))
        str = string.gsub(str, "#", n, 1)
    end
    return str
end

Examples:

print(randomNumbers("a#bcd")) -- a7bcd
print(randomNumbers("a#b#cd")) -- a6b5cd
print(randomNumbers("a#b#c#d")) -- a7b0c6d
print(randomNumbers("a#b#c#d#")) -- a3b7c4d5
print(randomNumbers("a#b#c#d##")) -- a1b0c9d71

strings = {
"1foo2bar3",
"foo1",
"bar1",
}

for i, str in ipairs (strings) do
    str = string.gsub(str, "(%d)", "#") -- replacing any digit by #
    print(randomNumbers(str))
end

Result:

7foo6bar5
foo7
bar0