forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Evaluate function?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
KcDan



Joined: 24 Jan 2006
Posts: 13
Location: Delaware

PostPosted: Thu Jan 26, 2006 12:57 pm    Post subject: Evaluate function? Reply with quote

I'm working on a calculator for my shell and I cant find anywhere if there is a function that evaulates a string and turns it into a number, for example

"5+6-2" -> 9
Back to top
View user's profile Send private message Visit poster's website AIM Address
youresam



Joined: 06 Nov 2005
Posts: 87

PostPosted: Thu Jan 26, 2006 1:29 pm    Post subject: Reply with quote

somenumber = tonumber("5+6-2")

then, somenumber should equal 9, I think.
Back to top
View user's profile Send private message AIM Address
KcDan



Joined: 24 Jan 2006
Posts: 13
Location: Delaware

PostPosted: Thu Jan 26, 2006 1:59 pm    Post subject: Reply with quote

I tried that earlier and it did not work, thanks though.
Back to top
View user's profile Send private message Visit poster's website AIM Address
ema



Joined: 07 Dec 2005
Posts: 11

PostPosted: Fri Jan 27, 2006 12:17 am    Post subject: Reply with quote

hi,

Code:
expression = "5+6-2"

script = "return " .. expression -- convert to LUA script
func, err = loadstring(script)
if err then
  -- syntax error
else
  result = func()    -- now result == 9
end



will work.
Back to top
View user's profile Send private message Visit poster's website
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Fri Jan 27, 2006 3:06 am    Post subject: Reply with quote

Also, the following will work:
Code:
script = "return 5+6-2"
result = assert(loadstring(script))()

Ref: Lua 5 Ref, page 40

Ema, you must have read PIL book well. :)

Hey, guys out there .. hint, hint
_________________
Geo Massar
Retired Engineer
Back to top
View user's profile Send private message Send e-mail
KcDan



Joined: 24 Jan 2006
Posts: 13
Location: Delaware

PostPosted: Fri Jan 27, 2006 11:37 am    Post subject: Reply with quote

Thanks a lot you two.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 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


Powered by phpBB © 2001, 2005 phpBB Group