Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Question for Lua replacing one letter with specific location

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
st_of_dec
Newbie cheater
Reputation: 0

Joined: 04 Jan 2018
Posts: 10
Location: AUS

PostPosted: Wed Mar 13, 2019 10:18 am    Post subject: Question for Lua replacing one letter with specific location Reply with quote

Hi guys,
This question is more like pure LUA question, rather than celua.
Btw, thank Darkbyte for solving my problem last time.

Here we go. I got a script with 100 thousands letters, and I want to change one letter in a specific location. I have already got the location by using string.find() function, then I have totally no idea how to deal with it.


For instance,
Code:

local s = [[ayzihblol,{A,3,+,%-,qiwhoiqwjnkgoiuapuj,lol,oisouhnqlolqppoz,lol,hjfapsqhuoqwjs]]

s1 = string.find(s,"iuapuj,") -- here is how I got the location

-- my result would be "ayzihblol,{A,3,+,%-,qiwhoiqwjnkgoiuapuj,XXX,oisouhnqlolqppoz,lol,hjfapsqhuoqwjs"
-- which I replace the first separate lol into XXX




I guess in this case, string.gsub() is out of use, because it is not only for global replacing, but also the module string is too difficult.



Regards.

_________________
gta5 creator st_of_dec
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25295
Location: The netherlands

PostPosted: Wed Mar 13, 2019 11:03 am    Post subject: Reply with quote

perhaps
string.sub(s,1,s1-1)..newstuff..string.sub(s,s1+1)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Wed Mar 13, 2019 11:18 am    Post subject: Reply with quote

If it's still just the first one you need to replace "gsub" will work.

http://lua-users.org/wiki/StringLibraryTutorial
Code:
local s = "Hello World, Hello World."
local s2 = string.gsub(s, "Hello", "Goodbye", 1)
print(s2)

Quote:

Goodbye World, Hello World.



If you prefer placeholders, this is what I use.
Code:

function interp(s, tbl)
   if s == nil then return end
   return (s:gsub('($%b{})', function(w) return tbl[w:sub(3, -2)] or w end))
end

local s = "Hello World, ${Greeting} World."
local s2 = interp(s, { Greeting = "Goodbye" })
print(s2)

Quote:

Hello World, Goodbye World.

_________________
Back to top
View user's profile Send private message Visit poster's website
st_of_dec
Newbie cheater
Reputation: 0

Joined: 04 Jan 2018
Posts: 10
Location: AUS

PostPosted: Thu Mar 14, 2019 4:49 am    Post subject: Reply with quote

Dark Byte wrote:
perhaps
string.sub(s,1,s1-1)..newstuff..string.sub(s,s1+1)


Thanks for solving my question again.
Here is an example of my final code.

Code:

local s = "aaa".."bbb".."ccc"
print(s)
local lol = string.find(s,"b",1)
s = string.sub(s,1,lol-1).."ddd"..string.sub(s,lol+3)
print(s)

_________________
gta5 creator st_of_dec
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites