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 


From 0 or 1 start?

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

Joined: 10 Feb 2018
Posts: 40

PostPosted: Sun Mar 04, 2018 2:17 am    Post subject: From 0 or 1 start? Reply with quote

The initial value of the array from 0 or 1 from the beginning?
form 0
a={1,2}
table.insert(a,3)
for i=0,#a do
print(a[i])
end

it prints
1
2
3


from 1
a={1,2}
table.insert(a,3)
for i=1,#a do
print(a[i])
end

it also prints
1
2
3

i am comfused,,,

Crying or Very sad

_________________
A wild programmer
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 14

Joined: 24 Feb 2017
Posts: 441
Location: 'Merica

PostPosted: Sun Mar 04, 2018 2:58 am    Post subject: Reply with quote

Code:
a={1,2}
table.insert(a,3)
for i=0,#a do
  print(type(a[i]))
  print(tostring(a[i]))
end


Quote:
nil
nil
number
1
number
2
number
3


And
Code:
print(nil)

Quote:



You just missed the blank line is all.

Some more on print:
Code:
a={1,2}
table.insert(a,3)
print(tostring(a))

Quote:
table: 0000000007276330


Code:
a={1,2}
table.insert(a,3)
print(a)

Quote:



Use the "type" and "tostring" functions when debugging/testing.

For a "short hand" of sorts I do some thing like this.
Code:
a={1,2}
table.insert(a,3)
print(1, a)

Quote:
1


Code:
a={1,2}
table.insert(a,3)
for i=0,#a do
  print(i, a[i])
end

Quote:
0
1 1
2 2
3 3

This just always gives you some thing on each line and then you can change the numbers and investigate as needed.

_________________
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 619

PostPosted: Sun Mar 04, 2018 4:45 am    Post subject: Reply with quote

also check table length

Code:
a={1,2}
table.insert(a,3)

local count = 0
for _ in pairs(a) do count = count + 1 end
return count
print(count)

for k,v in pairs(a) do  print(k, v) end

-- result
3
1 1
2 2
3 3

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater
Reputation: 32

Joined: 09 Aug 2013
Posts: 769

PostPosted: Sun Mar 04, 2018 6:36 am    Post subject: Reply with quote

when creating your own tables don't put stuff at index 0, reason:

Code:
sometable = {}
sometable[0] = "Catch me if you can! ~Frank Abagnale Jr."
sometable[#sometable+1] = "First!" -- #sometable = 0, thus 1
sometable[#sometable+1] = "Second"
sometable[#sometable+1] = "Third..."

for k,v in ipairs(sometable) do
  print(k,v)
end
--print(sometable[0])
result:
Code:
1 First!
2 Second
3 Third...


Same with pairs except it'll work with non-number indexes and it's not guaranteed to be ordered (or even have the same order each time when no changes have been made).

Though if you're using the lua engine window to test you can simply return the table rather than looping over it and get
Code:
:table
[
   1 = First!
   2 = Second
   3 = Third...
   0 = Catch me if you can! ~Frank Abagnale Jr.
]
up to about 10 entries... (for more than that you'd need to loop manually, or create some serialization function).

Or for personal debugging use you can add something like https://github.com/pkulchenko/serpent to the autorun list (you'll likely need to modify the module so instead of returning a table it sets a global variable) then you could do
Code:
print(serpent.block(sometable),'\r\n')
print(serpent.line(sometable),'\r\n')
print(serpent.dump(sometable),'\r\n')
and get
Code:
{
  "First!",
  "Second",
  "Third...",
  0,
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10,
  11,
  12,
  13,
  14,
  15,
  16,
  17,
  18,
  19,
  20,
  21,
  22,
  23,
  24,
  25,
  26,
  27,
  28,
  29,
  30,
  [0] = "Catch me if you can! ~Frank Abagnale Jr."
} --[[table: 0000000004C717E0]]
 
{"First!", "Second", "Third...", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, [0] = "Catch me if you can! ~Frank Abagnale Jr."} --[[table: 0000000004C717E0]]
 
do local _={[1]="First!",[2]="Second",[3]="Third...",[4]=0,[5]=1,[6]=2,[7]=3,[8]=4,[9]=5,[10]=6,[11]=7,[12]=8,[13]=9,[14]=10,[15]=11,[16]=12,[17]=13,[18]=14,[19]=15,[20]=16,[21]=17,[22]=18,[23]=19,[24]=20,[25]=21,[26]=22,[27]=23,[28]=24,[29]=25,[30]=26,[31]=27,[32]=28,[33]=29,[34]=30,[0]="Catch me if you can! ~Frank Abagnale Jr."};return _;end


edit: actually that first one doesn't get new lines in the output but that's probably because it wants \r\n not just \n... could probably modify that in the serpent file or use gsub before printing Smile

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
movss
Cheater
Reputation: 0

Joined: 10 Feb 2018
Posts: 40

PostPosted: Sun Mar 04, 2018 8:57 am    Post subject: Reply with quote

TheyCallMeTim13 wrote:
[code]a={1,2}



tks,i know

_________________
A wild programmer
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