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 


Sort Arry after using velue between

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

Joined: 16 Aug 2013
Posts: 26

PostPosted: Sat Sep 24, 2016 1:25 am    Post subject: Sort Arry after using velue between Reply with quote

I am first searching values then I want to print those values. And those values must b sorted. I using table.sort but not getting sorted result. Below is my code
Code:
    local memscan = createMemScan()
    local foundlist = createFoundList(memscan)
    memscan.firstScan(
      soValueBetween, vtDword, rtRounded,
      1, 99, 0, 0xffffffffffffffff, "+W-C",
      fsmAligned, "4", false, false, false, false)
    memscan.waitTillDone()
    foundlist.initialize()
    local values = {}
    local value = foundlist.Value

    for i = 0, foundlist.Count - 1 do
      values[value[i]] = true
    end

    foundlist.destroy()
    memscan.destroy()

    table.sort(values)
    for i in pairs(values) do
      print(i)
    end
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 941

PostPosted: Sat Sep 24, 2016 6:55 am    Post subject: Reply with quote

Lua table can be used as hash map or ordered linear array depending on how the operation see the table key.

As a hash map, operation like pairs/next in for statement will see all keys, but its order in for loop is unorder.

As an ordered liner array, operation like ipairs/sort, '#' for getting array length, can only see from numeric/integral key 1 to consecutive existed integer 'number' type keys.

For example, a literal Lua table with explicit key-value pairs
Code:

local t = {
 stringKey = 'non-numeric key cannot be seen in array',
 [0] = 0,
 [1] = 1,
 [2] = 2,
 [3] = 3,
 [4.5] = 4.5,
 [10] = 10,
}
print('pairs loop in all keys, unordered')
for k,v in pairs(t) do
  print(k,' = ',v)
end
print('')
print('ipairs loop in consecutive integer key from 1, ordered')
for i,v in ipairs(t) do
  print(i,' = ',v)
end


The ipairs stop at key 3 because the next consecutive integer key 4 is not existed in the table.


So the table.sort is not working on values since its key is all 'string' type.

To collect the foundlist.Value as array , try following, but before that note that the element content of Value is of 'string' type, '9' > '10' (string type compare) vs 9 < 10 (number type compare), so if the Value is numeric in nature, it should convert to appropriate type 1st, the conversion may depend on if the value is 'show as hex' during scan. The following assume the value is non-hex numbers format.

Code:

    for i = 0, foundlist.Count - 1 do
      values[i+1] = tonumber(foundlist.Value[i])  -- 1st key is (0+1) = 1
    end


bye~

_________________
- Retarded.
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sat Sep 24, 2016 8:28 am    Post subject: Reply with quote

Code:
local sorted = {}
for i in pairs(values) do
  sorted[#sorted+1] = i
end
table.sort(sorted)
for i, v in ipairs(sorted) do
  print(v)
end
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