|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Sat Jun 17, 2017 2:38 pm Post subject: (Lua) How to find all instances of a string efficiently? |
|
|
What is the most efficient way to find all instances of a string in a string?
For example:
Code: |
str = "hello world hello world hello world hello world hello world hello world hello world hello world"
|
How to find the positions of each "hello" effectively? Thanks.
Edit:
I tried "string.gmatch", but it can't return the index/position.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4289
|
Posted: Sat Jun 17, 2017 4:52 pm Post subject: |
|
|
If you want something simple, you could do this:
Code: | function basic_substr_iter(src, search)
local i = 0
return function()
i = string.find(src, search, i+1, true)
return i
end
end
for i in basic_substr_iter('abbacababa','ab') do
print(i) -- prints index of occurrence
end |
I don't know what algorithm string.find uses. If it's not fast enough for you, Google "string search algorithms".
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Sat Jun 17, 2017 5:34 pm Post subject: |
|
|
ParkourPenguin wrote: | If you want something simple, you could do this:
Code: | function basic_substr_iter(src, search)
local i = 0
return function()
i = string.find(src, search, i+1, true)
return i
end
end
for i in basic_substr_iter('abbacababa','ab') do
print(i) -- prints index of occurrence
end |
I don't know what algorithm string.find uses. If it's not fast enough for you, Google "string search algorithms". |
Thanks, Penguin. Can you explain a little bit about the third and fourth parameter of the "string.find" function?
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4289
|
Posted: Sat Jun 17, 2017 8:57 pm Post subject: |
|
|
Quote: | string.find (s, pattern [, init [, plain]])
Looks for the first match of pattern (see §6.4.1) in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. A third, optional numeric argument init specifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered magic. Note that if plain is given, then init must be given as well. | source
Basically, the third parameter is the index at which string.find should start looking (inclusive), and the fourth parameter is a boolean indicating if string.find shouldn't bother with magic characters in the pattern being searched for (e.g. %d, %s, %x, etc.). The only reason why I set that to true is because it would allow string.find to run faster. Whether it does or not I didn't test.
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Sun Jun 18, 2017 9:48 am Post subject: |
|
|
ParkourPenguin wrote: | Quote: | string.find (s, pattern [, init [, plain]])
Looks for the first match of pattern (see §6.4.1) in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. A third, optional numeric argument init specifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered magic. Note that if plain is given, then init must be given as well. | source
Basically, the third parameter is the index at which string.find should start looking (inclusive), and the fourth parameter is a boolean indicating if string.find shouldn't bother with magic characters in the pattern being searched for (e.g. %d, %s, %x, etc.). The only reason why I set that to true is because it would allow string.find to run faster. Whether it does or not I didn't test. |
Thanks for the explanation, Penguin.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
|
|
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
|
|