Author Topic: Lua Q&A  (Read 105406 times)

0 Members and 1 Guest are viewing this topic.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Lua Q&A
« Reply #150 on: July 17, 2013, 06:04:18 am »
From the manual:
"Metatables control the operations listed next. Each operation is identified by its corresponding name. The key for each operation is a string with its name prefixed by two underscores, '__'; for instance, the key for operation "add" is the string "__add"."

It's like operator overloading. You can define custom behaviour this way.

I'm not 100% sure as it's been a long time since I've done anything with Lua. I also think it had something to do with global variables as well, but that may not be true.

[Edit:] I meant not sure :P
« Last Edit: July 21, 2013, 09:03:43 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #151 on: July 17, 2013, 11:05:08 pm »
How do you reference a particular value in an array, like a matrix (such as in TI-BASIC, Casio Basic)

Here's an example array:

Code: [Select]
eg = {{1,1,1,2,2,1,3,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,2,2,2,3,1,3,3,1}
,{1,1,2,2,2,1,3,3,1,1,2,3,3,3,3,3,1,1,1,2,2,2,2,2,3,3,3,3,3,3,1}
,{1,1,3,3,3,1,1,1,3,3,3,3,3,1,1,1,2,2,2,2,2,3,3,3,3,3,3,1,2,2,2}
,{1,1,3,3,3,1,3,3,3,2,2,2,1,3,1,2,3,1,3,2,3,1,3,2,1,3,3,1,1,2,1}
,{2,2,3,3,3,3,3,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,1,1,1}
,{2,1,3,2,3,1,3,3,1,1,2,2,2,2,1,1,1,1,1,2,1,2,2,3,3,1,2,3,1,2,3}
,{3,3,1,1,1,2,2,2,3,1,1,3,2,1,3,2,1,1,1,2,2,1,2,2,3,2,3,3,3,3,1}
,{3,1,2,3,3,2,1,3,1,2,3,1,3,1,2,2,1,2,1,2,3,1,2,3,1,2,3,1,2,3,1}}
How would I for example reference the 1 in the 4th row, 6th column?

Or am I not thinking in Lua programming terms and this shouldn't be used?

I've tried eg[4][6] and eg [4,6] but they didn't seem to work.
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #152 on: July 17, 2013, 11:23:59 pm »
Never mind I figured it out. The correct way is
eg[4][6]

It was another bug in the code that was holding it back.
Sorry for double post.

PS: This code, I'll be releasing soon as a new project, but I don't want to reveal anything until I make further progress :)
« Last Edit: July 17, 2013, 11:28:48 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #153 on: July 21, 2013, 02:26:57 am »
So I think I need a bit more explanation with Lua classes. I got interested in them when I was trying to understand the owllibrary code. http://ourl.ca/14610

It had all this syntax that I didn't quite understand and the ordering and many cross-references between functions got me confused. So I read a bit of stuff on wiki, inspired-lua tutorials, some omnimaga topics and compasstech (not intently though, I kinda got it, but maybe not enough to use it)

so from what I gathered, here's an example with code taken from owllibrary.

Code: [Select]
Coyote = class()

function Coyote:init(x, y, width, height, options)
    options = options or {}
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.tiles = {}
    self.controls = options.controls or {up = "up",
                                         down = "down",
                                         left = "left",
                                         right = "right"}
    self.player = {}
    self.tileSize = options.tileSize or 16
    self.scrollThreshold = options.scrollThreshold or 1
    self.outOfBoundsColor = options.outOfBoundsColor or {0, 0, 0}
    self.tileCount = 0
end



Q1: This is more of a confirmation. Does self refers to the classname itself? So self.x would refer to Coyote.x in this case? Likewise for the others?

Q2: What does . refer to? From what I gather it would mean it would add it to a list/table so that if Coyote was called up, you'd get Coyote == {"x"==x, "y"==y, "tiles"=={} etc...}. Is this right?

Q3: How does or affect the variable assigning? in self.controls = options.controls or {up = "up", down = "down", left = "left", right = "right"}, does that mean self.controls becomes both possible values? This is one I'm stumped on

Q4: Also another one, I've never been very confident in what return means (even though I've encountered it in several languages such as JavaScript. I've known it to return a value if it refers to a variable but it seems that elsewhere in the code it can represent some other things too? Clarification?

Q5: Generally I don't understand classes enough, I don't think I finished reading through the inspired lua tutorial though. Might go back and read it ;D

Thanks for your help in advance.
« Last Edit: July 21, 2013, 02:27:37 am by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #154 on: July 21, 2013, 08:11:15 am »
Q1: This is more of a confirmation. Does self refers to the classname itself? So self.x would refer to Coyote.x in this case? Likewise for the others?
The real meaning of "self" is the first argument passed to the function. When using ":" instead of "." (i.e : foo:bar() ) the object (here "foo") is passed as frst parameter :
Code: [Select]
foo:bar() === foo.bar(foo)
Thus, it refers to the instance of the object using that function, but it can be used for other purposes, such as using a "static" method of the class (I may be unclear on that ... sorry) :
Code: [Select]
Foo = class()
function Foo:init()
  self.i = 69
end
function Foo:bar(i)
  print(i, self.i)
  self.i = i
end
foo = Foo()
foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69
foo:bar(42) -- prints 42 42
print(Foo.i)  -- nil
Foo:bar(42) -- calls Foo.bar(Foo, 42) ; prints 42 nil
print(Foo.i) -- prints 42

Q2: What does . refer to? From what I gather it would mean it would add it to a list/table so that if Coyote was called up, you'd get Coyote == {"x"==x, "y"==y, "tiles"=={} etc...}. Is this right?
Basic : classes in Lua are reprensented as tables.
"." let you access to one element (defined of not) of a table. In Lua tables are associatives using all sort of hash key (you can use functions or tables as hash key). In general you access an element using its key with [ and ] such as :
Code: [Select]
t = {foo=bar1, ["foo with spaces"]=bar2}
t["foo"] -- bar1
t["foo with spaces"] -- bar2
t.foo -- bar1
As you can see, since "foo" is a valid identifier, it can be used with the "." convention.

There are multiple ways to add an element in the table, and it depends if you're seeing the table as a contiguous array (such as in C) or associatives ones.
It depends when you're using #t or ipairs/pairs(t). For example IIRC you cannot use #t or table.getn(t) on tables you filled with . or [] although you can iterate through using ipairs/pairs.

So, as you may understood, this :
Code: [Select]
t = {}
t.x = 1
t.y = 2

t.x = t.x + 2

for k, v in pairs(t) do
  print(k, v) -- will print x 3 \n y 2
end
adds to t two elements, modify one and prints keys and values of each rows.

Q3: How does or affect the variable assigning? in self.controls = options.controls or {up = "up", down = "down", left = "left", right = "right"}, does that mean self.controls becomes both possible values? This is one I'm stumped on
"or" and "and" are used as shortcut to if statements.
The equivalent or the line you quoted is :
Code: [Select]
if not options.controls then
  self.controls = {up = "up",
                        down = "down",
                        left = "left",
                        right = "right"}
else
  self.controls = options.controls
end

Another example :

Code: [Select]
a = 1
b = nil
c = 3

d = c and a or b -- d = 1
d = a and b or c -- d = 3
d = b and a or c -- d = 3


Q4: Also another one, I've never been very confident in what return means (even though I've encountered it in several languages such as JavaScript. I've known it to return a value if it refers to a variable but it seems that elsewhere in the code it can represent some other things too? Clarification?
The main difference between return in Lua and return in Javascript is that in Lua's return, you can return multiple values (known as "tuple")
Code: [Select]
x, y = 42, 69
function getCoords()
  return x, y
end

function setCoords(xx, yy)
  x, y = xx + 1, yy + 1
end

x, y = getCoords()
print(getCoords())  -- prints 42 and 69
setCoords(getCoords())
print(getCoords())  -- prints 43 and 70



Q5: Generally I don't understand classes enough, I don't think I finished reading through the inspired lua tutorial though. Might go back and read it ;D
Classes in Lua are based on metatables that can be a nightmare if you don't understand their concept. Maybe you can read for that.
If you still don't understand metatable (I was not able to understand them at the beginning, but understood classes) think of classes in other languages.
The main difference is that you don't have virtual method walkthrough since when you inherit from another class all the references are copied and the one you override overrides the references in the metatable losing the reference to the parent class. The only way to use a parent class is either by modifying the class() function, or by knowning the name of the parent class and using Parent.method(child, args) bootstraping the ":" notation.
« Last Edit: July 22, 2013, 09:37:44 pm by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #155 on: July 22, 2013, 09:25:44 pm »
I had a response to this, but I lost it when I reloaded the page.
One thing: no wonder the class syntax is screwing me over. I'll have to try and slowly understand every line...
Code: [Select]
Foo = class()
function Foo:init()
  self.i = 69
end
function Foo:bar(i)
  print(i, self.i)
  self.i = i
end
foo = Foo()
foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69
foo:bar(42) -- prints 42 42
print(Foo.i)  -- nil
Foo:bar(42) -- calls Foo:bar(Foo, 42) ; prints 42 nil
print(Foo.i) -- prints 42
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #156 on: July 22, 2013, 09:38:39 pm »
I Edited my post :

Code: [Select]
-- Foo:bar(42) -- calls Foo:bar(Foo, 42) ; prints 42 nil
++ Foo:bar(42) -- calls Foo.bar(Foo, 42) ; prints 42 nil

I thought I had fixed it before but in fact, it somehow returned to the old version :p
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #157 on: July 26, 2013, 12:43:42 am »
Code: [Select]
Foo = class()
function Foo:init()
  self.i = 69
end
function Foo:bar(i)
  print(i, self.i)
  self.i = i
end
foo = Foo()
foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69
foo:bar(42) -- prints 42 42
print(Foo.i)  -- nil
Foo:bar(42) -- calls Foo.bar(Foo, 42) ; prints 42 nil
print(Foo.i) -- prints 42
This is too confusing. All the cross-referencing and assigning a var to another var, then using them in combination is obscuring the meaning for me. I rarely like to do this in my own programming.
Code: [Select]
foo = Foo()
Is this really needed?

Code: [Select]
foo:bar(42) == Foo():bar(42) -- that makes some sense now, does it call upon Foo() first, then Foo:bar(i) ? doesn't seem right
foo:bar(42) -- so the second time, because of self.i = i from the first time around, self.i == i
print(Foo.i) -- why is this nil? Is it because Foo:bar(i) took over the 'control' of self.i so Foo:init() doesn't control it anymore
Foo:bar(42)
print(Foo.i)

Now, you lost me.




EDIT:
Q2: What does . refer to? From what I gather it would mean it would add it to a list/table so that if Coyote was called up, you'd get Coyote == {"x"==x, "y"==y, "tiles"=={} etc...}. Is this right?
Basic : classes in Lua are reprensented as tables.
"." let you access to one element (defined of not) of a table. In Lua tables are associatives using all sort of hash key (you can use functions or tables as hash key). In general you access an element using its key with [ and ] such as :
Code: [Select]
t = {foo=bar1, ["foo with spaces"]=bar2}
t["foo"] -- bar1
t["foo with spaces"] -- bar2
t.foo -- bar1
As you can see, since "foo" is a valid identifier, it can be used with the "." convention.
What do you mean by a valid identifier? Does that mean with no spaces? Or does it cover something else as well?
« Last Edit: July 26, 2013, 12:51:57 am by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #158 on: July 26, 2013, 03:47:02 am »
Code: [Select]
foo = Foo()
Is this really needed?
If you're asking this question, you may not know that Lua is case sensitive.
Here, Foo is the class (or the "template to copy") and foo is the new object (or an "instance of this class").
Foo() is equivalent to "new Foo()" in some other POO languages.
Foo() calls Foo:init().

Quote
Q2: What does . refer to? From what I gather it would mean it would add it to a list/table so that if Coyote was called up, you'd get Coyote == {"x"==x, "y"==y, "tiles"=={} etc...}. Is this right?
Basic : classes in Lua are reprensented as tables.
"." let you access to one element (defined of not) of a table. In Lua tables are associatives using all sort of hash key (you can use functions or tables as hash key). In general you access an element using its key with [ and ] such as :
Code: [Select]
t = {foo=bar1, ["foo with spaces"]=bar2}
t["foo"] -- bar1
t["foo with spaces"] -- bar2
t.foo -- bar1
As you can see, since "foo" is a valid identifier, it can be used with the "." convention.
What do you mean by a valid identifier? Does that mean with no spaces? Or does it cover something else as well?
A valid identifier is basically a name you would give to a variable.
"foo_bar" is valid, "foo-bar" is not", "foo bar" is not, "foo/bar" is not, etc ...
« Last Edit: July 26, 2013, 03:48:22 am by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Q&A
« Reply #159 on: July 26, 2013, 08:04:41 am »
Well, here is an other example, hope it's a bit more clear.

So, lets say you have a car driving game and all the cars should have their own set of attributes. Managing this using classes is very easy, you start by creating your Car class, basically a prototype of your car or if said in another way, the mold you use to create all your cars.

Code: [Select]
Car = class()
function Car:init(model)
  self.model = model or "LuaCar"
  self.distance = 0
end

function Car:drive(d)
  self.distance = self.distance + d
  print(self.model .. " just drove " .. d .. " km")
end

function Car:totalDistance()
  print(self.model .. " drove " .. self.distance .. " km in total")
end

So, that's our class. Now a Car has two properties, a model name and a distance. Now lets make some Car objects and play with them.

Code: [Select]
myCar1 = Car("Fiat Ducato")
myCar2 = Car("Ford Fiesta")

myCar1:totalDistance()
myCar2:totalDistance()

print()

myCar1:drive(10)
myCar2:drive(5)

print()

myCar1:totalDistance()
myCar2:totalDistance()

print()

myCar2:drive(10)

print()

myCar1:totalDistance()
myCar2:totalDistance()

the output of this is

Code: [Select]
Fiat Ducato drove 0 km in total
Ford Fiesta drove 0 km in total

Fiat Ducato just drove 10 km
Ford Fiesta just drove 5 km

Fiat Ducato drove 10 km in total
Ford Fiesta drove 5 km in total

Ford Fiesta just drove 10 km

Fiat Ducato drove 10 km in total
Ford Fiesta drove 15 km in total

As you can see, myCar1 and myCar2 are total different instances (objects) of Car. You can use all the methods defined in the Car class. The 'self' refers to each unique instance.

As Levak said, the ':' isn't actually more than a short cut. Take for example the Car:drive method. You could also write it this way and it would work exactly the same:
Code: [Select]
function Car.drive(self, d)
  self.distance = self.distance + d
  print(self.model .. " just drove " .. d .. " km")
end

Now, if we call myCar1:drive(10), that would be the same as myCar1.drive(myCar1, 10). Through metatables the myCar1 table is linked to the Car table (object and classes are in the end still tables). So basically when you do myCar:drive(10) you do Car.drive(myCar1, 10).
« Last Edit: July 26, 2013, 08:05:36 am by Jim Bauwens »

Offline mannuri

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 12
  • Rating: +0/-0
    • View Profile
Re: Lua Q&A
« Reply #160 on: July 26, 2013, 04:25:22 pm »
Hi, Im triing make a program that need solve complex stuff, and i need to use nsolve( or solve( function since is for cas version.

here is my code:
Quote
cvi=cvi*10^(-7)
    ui=(qi*4)/(math.pi*di*di)
    rei=tostring((ui*di)/(cvi))
    di=tostring(di)
    ki=tostring(ki)
 
    formula = "yi^(−0.5)=−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)"

    var.store("formula", formula)
    equacao = "string(nSolve(expr(formula), yi)|yi<0.1 and yi>0"
    yi = math.eval(equacao)

When i go display, i got error.
Quote
attempt to concatenate global 'yi' (a nil value)

I have trie:
Quote
--whitout var.store("formula", formula)
equacao = "nSolve(formula, yi)|yi<0.1 and yi>0"

Please help me. thanks!

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Lua Q&A
« Reply #161 on: July 26, 2013, 04:38:37 pm »
Hi, Im triing make a program that need solve complex stuff, and i need to use nsolve( or solve( function since is for cas version.

here is my code:
Quote
cvi=cvi*10^(-7)
    ui=(qi*4)/(math.pi*di*di)
    rei=tostring((ui*di)/(cvi))
    di=tostring(di)
    ki=tostring(ki)
 
    formula = "yi^(−0.5)=−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)"

    var.store("formula", formula)
    equacao = "string(nSolve(expr(formula), yi)|yi<0.1 and yi>0"
    yi = math.eval(equacao)

When i go display, i got error.
Quote
attempt to concatenate global 'yi' (a nil value)

I have trie:
Quote
--whitout var.store("formula", formula)
equacao = "nSolve(formula, yi)|yi<0.1 and yi>0"

Please help me. thanks!

You never set a value to or declared yi before in your program, thus it has no value and is considered nil.
« Last Edit: July 26, 2013, 04:40:03 pm by jwalker »
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline mannuri

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 12
  • Rating: +0/-0
    • View Profile
Re: Lua Q&A
« Reply #162 on: July 27, 2013, 05:27:17 am »
Thanks!

I used the script:
Quote
rei=tostring((ui*di)/(cvi))
    di=tostring(di)
    ki=tostring(ki)
    yi="0.01"
    formula="yi^(−0.5)=−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)"
    yi="nSolve("..formula.."),yi)"

and i got the output display:
Quote
yi=nSolve(yi^(−0.5)=−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)),yi)
All is going good :)

but when i change the code to:
Quote
yi=math.eval("nSolve("..formula.."),yi)")
yi got nil value :(

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Q&A
« Reply #163 on: July 27, 2013, 06:24:35 am »
Not sure why this wouldn't work, but you can try storing temporarily the left and right side of the equation into a basic variable (with some other simpler math.eval("left:=xxxxx") etc.)
and then, math.eval("nSolve(left=right,yi)")

Edit : thanks to jim : indeed you have a ")" after the formula that will break everything :P remove it !
« Last Edit: July 27, 2013, 10:27:38 pm by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline mannuri

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 12
  • Rating: +0/-0
    • View Profile
Re: Lua Q&A
« Reply #164 on: July 28, 2013, 07:57:23 am »
thanks, but still got the same error.
Im using the ti cas teacher edition to edit and run the script. lastest version.
im using the TI Master Box Tool.
Quote
function Tipo1:Save()
    --Turn string into number--
    qi=math.eval(numberBoxQ.text)
    li=math.eval(numberBoxL.text)
    di=math.eval(numberBoxD.text)
    cvi=math.eval(numberBoxcv.text)
    ki=math.eval(numberBoxK.text)
    --If no imput variable get 0--
    if numberBoxQ.text == "" then
    qi=0
    end
    if numberBoxL.text == "" then
    li=0
    end
    if numberBoxD.text == "" then
    di=0
    end
    if numberBoxcv.text == "" then
    cvi=0
    end
    if numberBoxK.text == "" then
    ki=0
    end
    --Solving the problem--
    ui=(qi*4)/(math.pi*di*di)
    rei=(ui*di)/(cvi)
    --Using tostring to insert into formula--
    rei=tostring(rei)
    di=tostring(di)
    ki=tostring(ki)
    --Declaring yi close value from the real expected--
    yi="0.01"
    --full formula--
    math.eval("yi^(−0.5)=:fl")
    math.eval("−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)=:fr")
    --solving the formula--
    yi=math.eval("nSolve(fl=fr,yi)")
    --disp results--
    helpLaunch(Resultados1)
end
    --simple round--
function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

Resultados1 = class()

function Resultados1:init()
   
    labelBoxu = LabelBox("          U = "..round(ui,7).." m/s")
    labelBoxre = LabelBox("          Re = "..round(rei,7))
    --Got problem here--171: attempt to concatenate global 'yi' (a nil value)--
    labelBoxy = LabelBox("          λ = "..yi)

I have trie sneack into formula pro lua code, but i dont understand how the solve works there  <_<
maybe if the nsolver dont disp only 1 awsner yi got nil? how i solve it?

Sorry for the big post.
edit:change left and right to fl and fr, because left() is a nspire function. still got yi nil value

edit 2:
FINNALY!!!, i made it!
i just need var.store() all vars and its done.

Thank you guys i love you all!!
« Last Edit: July 28, 2013, 08:33:25 am by mannuri »