 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
icsmoke+ Cheater
Reputation: 0
Joined: 31 Aug 2020 Posts: 29
|
Posted: Tue Mar 29, 2022 10:14 pm Post subject: [ask] table sort |
|
|
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 |
|
 |
AylinCE Grandmaster Cheater Supreme
Reputation: 37
Joined: 16 Feb 2017 Posts: 1522
|
Posted: Wed Mar 30, 2022 2:32 am Post subject: |
|
|
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")) |
_________________
|
|
Back to top |
|
 |
panraven Grandmaster Cheater
Reputation: 62
Joined: 01 Oct 2008 Posts: 958
|
Posted: Wed Mar 30, 2022 4:02 am Post subject: |
|
|
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 |
|
 |
icsmoke+ Cheater
Reputation: 0
Joined: 31 Aug 2020 Posts: 29
|
Posted: Wed Mar 30, 2022 5:26 am Post subject: |
|
|
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 |
|
 |
|
|
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
|
|