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 


Why tonumber() returns nil?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Sat Jun 10, 2017 1:02 pm    Post subject: Why tonumber() returns nil? Reply with quote

Code:

List = createStringlist()                  <----create the string list

local f = io.open("d:\\ID_Info.txt","r") <-------- then get the file
io.input(f)
local str = io.read("*all")                  <-------- read the file into a string
for i in string.gmatch(str, "%S+") do        <---- get rid of whitespace
   List.add(i)                <-----------the string list is filled correctly
end
f:close()

print(List[0])             <-----this returns "1", which is correct
print(type(List[0]))           <----------- this returns string ,which is also correct
print(tonumber(List[0]))            <---------this returns nil, which is wrong, but why?????????????


I have been working on this for an hour, but still can't figure out the problem. Please take a look at the code, my question is in the comments, thanks a lot.

Edit: I fixed the problem by doing the following:
1. Add an empty line in the file;
2. use the number "1" as the first index.

The txt file looks like this:
1 12312321
2 234345432534
3 23432443
......
1036 234234324

The first column is the line number, the second is some string data.

_________________
**************

A simple example is better then ten links. Very Happy


Last edited by Dr.Disrespect on Sat Jun 10, 2017 2:42 pm; edited 3 times in total
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 218

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Sat Jun 10, 2017 2:40 pm    Post subject: Reply with quote

c:\ID_Info.txt contains 1500100900
Code:
List = createStringlist()

local f = io.open("c:\\ID_Info.txt","r")
local str = f:read("*all")
for i in string.gmatch(str, "%S+") do
   List.add(i)
end
f:close()

print(List[0])
print(type(List[0]))
print(tonumber(List[0]))


output:
Code:
1500100900
string
1500100900

_________________
Back to top
View user's profile Send private message MSN Messenger
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Sat Jun 10, 2017 2:43 pm    Post subject: Reply with quote

mgr.inz.Player wrote:
c:\ID_Info.txt contains 1500100900
Code:
List = createStringlist()

local f = io.open("c:\\ID_Info.txt","r")
local str = f:read("*all")
for i in string.gmatch(str, "%S+") do
   List.add(i)
end
f:close()

print(List[0])
print(type(List[0]))
print(tonumber(List[0]))


output:
Code:
1500100900
string
1500100900


Thanks for the reply. I think my problem was related to the content in the file. The content looked like this:
Code:

1             sadfadsf
2             234243
3             adfadf
.....
1036       32424324

The first column is the line number, the second is some data.

After I added one empty line at the beginning, and used "1" as the first index, the problem was gone.

_________________
**************

A simple example is better then ten links. Very Happy
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