|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
movss Cheater Reputation: 0
Joined: 10 Feb 2018 Posts: 38
|
Posted: Sun Mar 04, 2018 2:17 am Post subject: From 0 or 1 start? |
|
|
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,,,
_________________
A wild programmer |
|
Back to top |
|
|
TheyCallMeTim13 Wiki Contributor Reputation: 50
Joined: 24 Feb 2017 Posts: 976 Location: Pluto
|
Posted: Sun Mar 04, 2018 2:58 am Post subject: |
|
|
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
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) |
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) |
Code: | a={1,2}
table.insert(a,3)
for i=0,#a do
print(i, a[i])
end |
This just always gives you some thing on each line and then you can change the numbers and investigate as needed.
_________________
|
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Sun Mar 04, 2018 4:45 am Post subject: |
|
|
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 |
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Sun Mar 04, 2018 6:36 am Post subject: |
|
|
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
_________________
|
|
Back to top |
|
|
movss Cheater Reputation: 0
Joined: 10 Feb 2018 Posts: 38
|
Posted: Sun Mar 04, 2018 8:57 am Post subject: |
|
|
TheyCallMeTim13 wrote: | [code]a={1,2}
|
tks,i know
_________________
A wild programmer |
|
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
|
|