Can a boolean be included in an "if" statement? - lua

I'm just starting out on learning Lua (for 4 days now) and when running this code, I get an error: input:2: 'then' expected near '='
Here's the code I am using:
local imagineVar = true
if imagineVar = true then
print("LOL")
end
How do I fix this?

if imagineVar then
print("LOL")
end
in lua, anything in if statment will be true except false and nil

The error you're getting is a syntax error because assignments (var = something) are statements rather than expressions in Lua - that means they don't evaluate to a value and thus can't be used in an if-condition (or anywhere else where an expression is expected).
As others have pointed out, you'd use the operator == for comparison. It is however more idiomatic to check for truthiness if your variable is a boolean: if imagineVar then ... end; the body of the if will run only if imagineVar is not nil or false.

Compare needs double '='
local imagineVar = true
if imagineVar == true then
print("LOL")
end

Related

Lua behaves funny with string.match

Here is a very simple code that didn't work as expected :
function convert(str)
local _,name = string.match(str, [[<a href=(.*)>(%w+)</a>]])
return name
end
print(convert("A"))
print(convert("B"))
print(convert("C"))
I expected :
A
B
C
And got :
A
nil
nil
Can somebody explain me how Lua is working in this case ?
OK, got it...
I put sample values for StackOverflow. Real values had spaces. I transformed the function to
function convert(str)
local _,name = string.match(str, [[<a href=(.*)>(.*)</a>]])
return name
end

What am I doing wrong with my AI?

I've been programming an AI with Lua that you communicate with it in my own logical language. I stumbled across a problem and I can't seem to figure this out.
I'm trying to put y/n questions in. I pretty much said: mi=David la; (sets variable to David. la; is punctuation) la mi=David dor la; (Is 'mi' equal to 'David'?)
When I typed that into it, 'ROBO-DUDE' didn't say anything.
if v == "lol" then
local yes = true
for _,v in pairs(mode[2]) do
if v == false then
print(v)
yes = false
end
print(yes)
end
print(yes)
if yes == true then
things = things .. "jar; "
else
things = things .. "awa; "
end
end
This block of code is in a loop for the 'la' statement. 'dor' means to respond yes/no, the lexer changes it to 'lol'.
When I tested it, the code seemed to skip the dor/lol part of the loop. I went to check the lexer.
if v == "dor" then
sentence[#sentence+1] = "lol"
end
I have no clue what went wrong here. I would like somebody's help on this problem.
Nevermind. I found the problem. When I used a for loop, I used the variable 'v' for the main parser loop and the one that looped through another table/array. I believe changing the variable (any of them) will fix my issue.

Simple if else in rails

How to implement simple if else condition in rails
PHP : echo $params = isset($_POST['some_params']) ? $_POST['some_params'] : "";
RAILS : ??
Thanks,
You may want to look into Ruby Ternary operator: A good source is http://www.tutorialspoint.com/ruby/ruby_operators.htm
params[:some_params].present? ? 'a' : 'b'
another way of doing this is:
if params[:some_params].present?
....
else
....
end
There is one more operator called Ternary Operator. This first
evaluates an expression for a true or false value and then execute one
of the two given statements depending upon the result of the
evaluation. The conditional operator has this syntax:

what does ? ? mean in ruby [duplicate]

This question already has answers here:
How do I use the conditional operator (? :) in Ruby?
(7 answers)
Closed 7 years ago.
What does the line below checks and perform?
prefix = root_dir.nil? ? nil : File.join(root_dir, '/')
Here is the block that contains the line of code.
def some_name(root_dir = nil, environment = 'stage', branch)
prefix = root_dir.nil? ? nil : File.join(root_dir, '/')
.
.
.
i know that the '?' in ruby is something that checks the yes/no fulfillment. But I am not very clear on its usage/syntax in the above block of code.
Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false.
When you write a function that can only return true or false, you should end the function name with a question mark.
The example you gave shows a ternary statement, which is a one-line if-statement. .nil? is a boolean function that returns true if the value is nil and false if it is not. It first checks if the function is true, or false. Then performs an if/else to assign the value (if the .nil? function returns true, it gets nil as value, else it gets the File.join(root_dir, '/') as value.
It can be rewritten like so:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
This is called a ternary operator and is used as a type of shorthands for if/else statements. It follows the following format
statement_to_evaluate ? true_results_do_this : else_do_this
A lot of times this will be used for very short or simple if/else statements. You will see this type of syntax is a bunch of different languages that are based on C.
The code is the equivalent of:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
See a previous question

LUA: Seeking efficient and error-free means of assigning default arguments

Instead of using long lists of arguments in my function definitions, I prefer to pass a few fixed parameters and a table of 'additional params' like this:
function:doit( text, params )
end
This is nice as it allows me to add new named parameters later without breaking old calls.
The problem I am experiencing occurs when I try to force default values for some of the params:
function:doit( text, params )
local font = params.font or native.systemBold
local fontSize = params.fontSize or 24
local emboss = params.emboss or true
-- ...
end
The above code works fine in all cases, except where I have passed in 'false' for emboss:
doit( "Test text", { fontSize = 32, emboss = false } )
The above code will result in emboss being set to true when I really wanted false.
To be clear, what I want is for the first non-NIL value to be assigned to emboss, instead I'm getting a first non-false and non-NIL.
To combat this problem I wrote a small piece of code to find the first non-NIL value in a table and to return that:
function firstNotNil( ... )
for i = 1, #arg do
local theArg = arg[i]
if(theArg ~= nil) then return theArg end
end
return nil
end
Using this function I would re-write the emboss assignment as follows:
local emboss = firstNotNil(params.emboss, true)
Now, this certainly works, but it seems so inefficient and over the top. I am hoping there is a more compact way of doing this.
Please note: I found this ruby construct which looked promising and I am hoping lua has something like it:
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
Lua's relational operators evaluate to the value of one of the operands (i.e. the value is not coerced to boolean) so you can get the equivalent of C's ternary operator by saying a and b or c. In your case, you want to use a if it's not nil and b otherwise, so a == nil and b or a:
local emboss = (params.emboss == nil) and true or params.emboss
Not as pretty as before, but you'd only need to do it for boolean parameters.
[snip - Lua code]
Now, this certainly works, but it seems so inefficient and over the top.
Please note: I found this ruby construct which looked promising and I am hoping lua has
something like it:
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
Your Lua function is no more over-the-top or inefficient. The Ruby construct is more succinct, in terms of source text, but the semantics are not really different from firstNotNil(c,b,a). Both constructs end up creating a list object, initialize it with a set of values, running that through a function that searches the list linearly.
In Lua you could skip the creation of the list object by using vararg expression with select:
function firstNotNil(...)
for i = 1, select('#',...) do
local theArg = select(i,...)
if theArg ~= nil then return theArg end
end
return nil
end
I am hoping there is a more compact way of doing this.
About the only way to do that would be to shorten the function name. ;)
If you really want to do it in a single line, you'll need something like this for a default value of true:
local emboss = params.emboss or (params.emboss == nil)
It's not very readable, but it works. (params.emboss == nil) evaluates to true when params.emboss is not set (when you would need a default value), otherwise it's false. So when params.emboss is false, the statement is false, and when it's true, the statement is true (true or false = true).
For a default of false, what you tried originally would work:
local emboss = params.emboss or false

Resources