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 


[ask] table sort

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

Joined: 31 Aug 2020
Posts: 29

PostPosted: Tue Mar 29, 2022 10:14 pm    Post subject: [ask] table sort Reply with quote

Hello there, please help on table.sort i do googling but i can't find the answer for my needed to my script here is the script below
i want to list the value of kn(which is describe below) then sort it from ascending to descending the result my vary because Value always change

Code:

al = getAddressList()
getMr = al.getMemoryRecordByDescription
K = {(getMr('kn1')),(getMr('kn2')),(getMr('kn3')),(getMr('kn4')),(getMr('kn5')),(getMr('kn6')),(getMr('kn7')),(getMr('kn8')),(getMr('kn9'))}
for i = 1, 9 do print(K[i].Value) end

result when printed unsorted:
5439554
4849744
4325453
4390989
5111887
2752569
2752576
0
0

what i want is result should begin froma smallest to largest

refer to DB answer to this post https://forum.cheatengine.org/viewtopic.php?p=5777131&sid=f69ceb63c0038fb042258dbd181da47e i did change
Code:

K = {
            {(getMr('kn1'))},
            {(getMr('kn2'))},
            {(getMr('kn3'))},
            {(getMr('kn4'))},
            {(getMr('kn5'))},
            {(getMr('kn6'))},
            {(getMr('kn7'))},
            {(getMr('kn8'))},
            {(getMr('kn9'))}
           }
   table.sort(K, function(a,b)
     if a.Value[5439554]~=b[5439554] then
       return a[5439554]<b[5439554]
     else
       return a[2752569 ]<b[2752569 ]
     end


   end)

for i=1,K do
    print(K[i][2752569 ],K[i][5439554])
end

it gives me an error Error:[string "al = getAddressList()
..."]:4: attempt to index a nil value (field '?')
Script Error
thanks in advance
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1522

PostPosted: Wed Mar 30, 2022 2:32 am    Post subject: Reply with quote

In your case the code could be:

Ignores list comments.
It just sorts the values.

For example 1;
Code:
al = getAddressList()
getMr = al.getMemoryRecordByDescription
K = {(getMr('kn1')),(getMr('kn2')),(getMr('kn3')),(getMr('kn4')),(getMr('kn5')),(getMr('kn6')),(getMr('kn7')),(getMr('kn8')),(getMr('kn9'))}

function SorTbl(tbl)
T={}
al = getAddressList()
getMr = al.getMemoryRecordByDescription
 for i=1, #tbl do
  desVal=tbl[i].Value
  --print(desVal)
  T[i]=desVal
 end
 if T then
  table.sort(T)
 end
return T
end

K1=SorTbl(K)
minVal=K1[1]
maxVal=K1[#K1]
print("Min Value: " .. minVal .. "\nMax Value: " .. maxVal)
--for i = 1, 9 do print(K1[i]) end



For example 2;
Code:
al = getAddressList()
getMr = al.getMemoryRecordByDescription
K = {(getMr('kn1').Value),(getMr('kn2').Value),(getMr('kn3').Value),(getMr('kn4').Value),(getMr('kn5').Value),(getMr('kn6').Value),(getMr('kn7').Value),(getMr('kn8').Value),(getMr('kn9').Value)}

  table.sort(K)

minVal=K[1]
maxVal=K[#K]
print("Min Value: " .. minVal .. "\nMax Value: " .. maxVal)
--for i = 1, 9 do print(K[i]) end



Code:
al = getAddressList()
getMr = al.getMemoryRecordByDescription
K = {{"kn1",(getMr('kn1').Value)},
     {"kn2",(getMr('kn2').Value)},
     {"kn3",(getMr('kn3').Value)},
     {"kn4",(getMr('kn4').Value)},
     {"kn5",(getMr('kn5').Value)},
     {"kn6",(getMr('kn6').Value)},
     {"kn7",(getMr('kn7').Value)},
     {"kn8",(getMr('kn8').Value)},
     {"kn9",(getMr('kn9').Value)}}

     table.sort(K, function(a,b) return a[2]<b[2] end)

for i = 1, 9 do print(K[i][1],K[i][2]) end


With list descriptions:
Code:
al = getAddressList()
getMr = al.getMemoryRecordByDescription
K = {{"kn1",(getMr('kn1').Value)},
     {"kn2",(getMr('kn2').Value)},
     {"kn3",(getMr('kn3').Value)},
     {"kn4",(getMr('kn4').Value)},
     {"kn5",(getMr('kn5').Value)},
     {"kn6",(getMr('kn6').Value)},
     {"kn7",(getMr('kn7').Value)},
     {"kn8",(getMr('kn8').Value)},
     {"kn9",(getMr('kn9').Value)}}

     table.sort(K, function(a,b) return a[2]<b[2] end)

for i = 1, 9 do print(K[i][1],K[i][2]) end

print("Value Max: " .. K[9][1],K[9][2])

function DescValue(tbl,desc)
res=""
 for i,k in pairs(tbl) do
   if k[1]==desc then
    --print(1,k[1])
    res=k[2]
   end
 end
 return res
end

print("Value Desc kn4: " .. DescValue(K,"kn4"))

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 958

PostPosted: Wed Mar 30, 2022 4:02 am    Post subject: This post has 1 review(s) Reply with quote

1. sort in 'string' type is different from sort in 'number' type, for instant string'123' < string '99' while number 123 > number 99, and memory record .Value is of string type ; and it can be '??' (unreadable etc);
To sort numberically, may use tonumber function and should handle unreadable.
eg. this sort ascending and move unreadable to last (replace by a larger number); table use last K = {memroy-records...} and assume the records exist (if no typo etc)
Code:

local unreadable = math.huge -- or math.maxinteger (and -math.huge / math.mininteger for decending sort)
table.sort(K, function(a, b)
  local va = tonumber(a.Value) or unreadbable
  local vb = tonumber(b.Value) or unreadbable
  return va < vb
end)

in general, a,b should convert parallelly before the comparison.


2. the link you refer to, later code by DB, is a kind of multi level sorting, do you really need it?

3. what is the actual effect of the sorting you want? eg. re-arrange your memory record or just printing?

_________________
- Retarded.
Back to top
View user's profile Send private message
icsmoke+
Cheater
Reputation: 0

Joined: 31 Aug 2020
Posts: 29

PostPosted: Wed Mar 30, 2022 5:26 am    Post subject: Reply with quote

thanks aylin and panraven i will try to learn and understand the code you gives,

to panraven for number 3. i need to reach the smallest value to re arrange my memory record and use it to another function

and the code is work as i expected
to mr panraven here the result with your code, now how to not include 0 value on result?

0
0
0
0
3735616
4587597
4653140
5439565
7077944
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