Jump to content
  • 0

How to group values to edit in a search?


ThienTeakee
 Share

Question

Hi guys, I'm new at GG

I have a script like this

gg.clearResults()

gg.setRanges(gg.REGION_ANONYMOUS)
gg.searchNumber('1.014.350.479;1.012.202.996;1.083.179.008;1.050.253.722;1.031.127.695;1.065.353.216;1.065.353.216;1.067.282.596:61', gg.TYPE_DWORD, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(5000)

I want to change "1.014.350.479" to "1.011.011.011" and "1.050.253.722;1.031.127.695" to "1". Because I only know how to edit all of them with "editAll()", not know how to group them like this.

Thanks for your help

Link to comment
Share on other sites

Recommended Posts

  • 0
  • Administrators
1 minute ago, CmP said:

edited[#edited + 1] = {address = v.address, flags = v.flags, value = 1011011011}

better:

v.value = 1011011011
edited[#edited + 1] = v

do not waste time and memory for duplicate same table.

Link to comment
Share on other sites

  • 0

Didn't notice that 

gg.setRanges(gg.REGION_ANONYMOUS)
gg.searchNumber('1014350479;1012202996;1083179008;1050253722;1031127695;1065353216;1065353216;1067282596:61', 4) -- search
local t1 = gg.getResults(5000)
for i = 1 ,#t1 do
if(t1[i].value==1014350479)then
    t1[i].value = 1011011011
    elseif(t1[i].value==1050253722 or t1[i].value ==1031127695)then
    t1[i].value = 1
	end
end
gg.setValues(t1)

 

Edited by ItsSC
Removed garbage code. Move set values out from loop.
Link to comment
Share on other sites

  • 0
  • Administrators

Strictly speaking fastest is reuse results table:

local results = gg.getResults(5000)
for i, v in ipairs(results) do
  if v.value == 1014350479 then
    v.value = 1011011011
  elseif v.value == 1050253722 or v.value == 1031127695 then
    v.value = 1
  else
    results[i] = nil
  end
end
gg.setValues(results)
results = nil

 

Link to comment
Share on other sites

  • 0
  • Administrators
11 minutes ago, ItsSC said:

for i = 1 ,#t1 do

better use ipairs for speed. or you need make local variable for store

t[i]

. Index over table is not a free. Better use it one time per loop iteration if it is possible.

for i, v in ipairs(results) do

Already do it for you.

Link to comment
Share on other sites

  • 0

Oh, that's a good idea, haven't thought about it. Just like it was mentioned few posts above, another thing has been learned. It's amazing that new things can be learned even about the topics that looks easy.

Edited by CmP
Minor grammatical correction
Link to comment
Share on other sites

  • 0
  • Administrators
17 minutes ago, Enyby said:

results[i] = nil

this line erase element from table. So setValues do not re-set values and we avoid possibility erase changed value with old one.

Also this operation really fast if run on list table (array-like table, called sequence in lua manual). And this is not rebuild table or reallocate memory, just exclude one value.

So this way really fast. But if you try (after few remove) add to table some new value, or remove value from hash part - it can call rebuild table with memory reallocation, which is slow.

Link to comment
Share on other sites

  • 0
1 minute ago, Enyby said:

this line erase element from table. So setValues do not re-set values and we avoid possibility erase changed value with old one.

Also this operation really fast if run on list table (array-like table, called sequence in lua manual). And this is not rebuild table or reallocate memory, just exclude one value.

So this way really fast. But if you try (after few remove) add to table some new value, or remove value from hash part - it can call rebuild table with memory reallocation, which is slow.

So basically, it runs checking for all results at once, while my method is checking one by one? 

Link to comment
Share on other sites

  • 0
29 minutes ago, Enyby said:

Iterate over results, build table and pass it to setValues.

Best solution if you have small amount of results.

 

25 minutes ago, CmP said:

The group search is not ordered, so I would prefer second approach from those Enyby mentioned. For this case it would be something like the following: 

So that the only way is build a table and do a loop with if & elseif to edit? If this is PHP I will do that smoothly 😂

Link to comment
Share on other sites

  • 0
  • Administrators
4 minutes ago, Enyby said:

call rebuild table with memory reallocation, which is slow.

By the way, for this reason, this code:

local t = {1, 2, 3, ..., 1000}

much faster than this:

local t = {}
t[1] = 1
t[2] = 2
t[3] = 3
...
t[1000] = 1000

For the table, an array of the right size is immediately allocated, and not reallocated repeatedly as it is populated.
Although, again, this is true when the number of elements is thousands and tens of thousands, and not when there are hundreds of them. In this case, there will be no apparent difference.

Link to comment
Share on other sites

  • 0
26 minutes ago, Enyby said:

Strictly speaking fastest is reuse results table:


local results = gg.getResults(5000)
for i, v in ipairs(results) do
  if v.value == 1014350479 then
    v.value = 1011011011
  elseif v.value == 1050253722 or v.value == 1031127695 then
    v.value = 1
  else
    results[i] = nil
  end
end
gg.setValues(results)
results = nil

 

yup....just put it in my script.

its goddam so fast.

Link to comment
Share on other sites

  • 0
  • Administrators
4 minutes ago, ItsSC said:

So basically, it runs checking for all results at once, while my method is checking one by one? 

No. Any loop work one-by-one. But you do same work again and again.

You can compile both examples to LASM and compare count of instructions.

You will be unpleasantly surprised by the number of instructions in your code.

for i = 1 ,#t1 do
if(t1[i].value==1014350479)then -- get t1[i]
    t1[i].value = 1011011011 -- get t1[i] again
    elseif(t1[i].value==1050253722 or t1[i].value ==1031127695)then -- get t1[i] two times
    t1[i].value = 1 -- get t1[i] again
	end
end

Lua do not optimize any code. So if you write get same value few times - lua do it for you. With decrease performance, because it is pointless work.

Link to comment
Share on other sites

  • 0
10 minutes ago, ThienTeakee said:

So that the only way is build a table and do a loop with if & elseif to edit?

It's not the only way, but it is suitable for your case. The most efficient implementation of this way from the ones proposed in this topic can be found in this post: 

How to group values to edit in a search? (#2mhgpa6)

It reuses the table returned by "getResults" function that leads to better performance of the code, but as it was mentioned above, it does not really matter when results count is relatively small.

Edited by CmP
Link to comment
Share on other sites

  • 0
  • Administrators
6 minutes ago, zam535582 said:

its goddam so fast.

You can make it is faster, by cache get 'value' from inner table:

local results = gg.getResults(5000)
for i, v in ipairs(results) do
  local value = v.value
  if value == 1014350479 then
    v.value = 1011011011
  elseif value == 1050253722 or value == 1031127695 then
    v.value = 1
  else
    results[i] = nil
  end
end
gg.setValues(results)
results = nil

 

Link to comment
Share on other sites

  • 0
6 minutes ago, Enyby said:

No. Any loop work one-by-one. But you do same work again and again.

You can compile both examples to LASM and compare count of instructions.

You will be unpleasantly surprised by the number of instructions in your code.


for i = 1 ,#t1 do
if(t1[i].value==1014350479)then -- get t1[i]
    t1[i].value = 1011011011 -- get t1[i] again
    elseif(t1[i].value==1050253722 or t1[i].value ==1031127695)then -- get t1[i] two times
    t1[i].value = 1 -- get t1[i] again
	end
end

Lua do not optimize any code. So if you write get same value few times - lua do it for you. With decrease performance, because it is pointless work.

Aight, this refreshed my knowledge of how the loop actually works. Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.