How to check if the (+X) last text - lua

I have the following code:
local text = 'CIA'
if text == text..'+X' then
print 'true'
else
print 'false'
end
I want check if last text ends with ('+X')

The string.sub function extracts a substring. Negative indices start from the end.
if string.sub(text, -2) == '+X' then
-- Ends with +X, do stuff accordingly.
end

Related

How can I use conditional statements to manipulate information while using io.write?

I want the player to be able to choose a character so I print a list of four characters.
print("Choose a character")
print("1. the rocker")
print("2. the vocalist")
print("3. the drummer")
print("4. the bassist")
I then use the io.write function to allow the player to make a choice between characters 1 and 4. I save the choice in the menu_option variable. I know that I would have to add some code for error handling but I am not worried about that at the moment
io.write('Which character do you choose?')
menu_option = io.read()
I now want to create some conditionals to create a variable that will define the title of the character that the player chose.
if menu_option == 1 then
character = ("the rocker")
elseif menu_option == 2 then
character = ("the vocalist")
elseif menu_option == 3 then
character = ("the drummer")
elseif menu_option == 4 then
character = ("the bassist")
end
This is where my code begins to fail. The write function is correctly writing the choice (from 1 to 4) to the menu_option variable but my if statement block is not correctly functioning. The character variable remains nil.
What am I doing wrong? Thanks for any help you all can give me.
The wrong is that io.read() always returning a string.
You expected a number in your if conditions.
Now you have to correct each if like #Egor wrote in the comment or do...
menu_option = tonumber(io.read())
...and let the if' check for numbers
After that you can do in case of NaN (Not a Number) or entering nothing (i.e. hit only RETURN/ENTER)...
io.write('Which character do you choose? ')
local menu_option = tonumber(io.read())
if menu_option == empty then menu_option = math.random(4) end
-- empty == nil but better (human) readable
...for a random selection.
Moreover i suggest using more local variable declarations so it looks like...
-- File: character.lua
local character = ''
print("Choose a character:")
print("[1] the rocker")
print("[2] the vocalist")
print("[3] the drummer")
print("[4] the bassist")
io.write('Which character do you choose? ')
local menu_option = tonumber(io.read())
if menu_option == empty then menu_option = math.random(4) end
if menu_option == 1 then
character = "the rocker"
elseif menu_option == 2 then
character = "the vocalist"
elseif menu_option == 3 then
character = "the drummer"
elseif menu_option == 4 then
character = "the bassist"
end
print('You choosed:',character:upper())
-- Possible return values...
-- return character:upper(), menu_option -- Two values: first=string second=number
-- return os.exit(menu_option) -- Number can examined on bash with ${?}
-- ^-> Example: lua character.lua || printf '%d\n' ${?}

Lua code elseif not working

This is all about accepting input from user and searching with that particular text.
Playing with string.gsub.
io.write("ENTER ANY STORY :-D ")
story=io.read()
io.write("\t OKAY!, THAT'S NICE :-D ")
io.write("\t DO YOU WANT TO REPLACE ANY TEXT:? ")
accept=io.read()
if accept=="YES" or "yes" then
io.write("\t WHICH TEXT TO REPLAE? ")
replace=io.read()
--HERE IS THE REPLACING TEXT
io.write("\t WITH WHAT:? ")
with=io.read()
result=string.gsub(story,replace,with)
print("\t THE REPLACED TEXT IS: ",result)
elseif accept=="NO" or "no" then
print(result)
end
Bug: The elseif loop isn't working!!
== and or work like mathematical operators in that they are evaluated one at a time, with the == being evaluated first. If accept is 'no', accept=="YES" or "yes" will be evaluated like this:
(accept == "YES") or "yes"
('no' == "YES") or "yes"
false or "yes"
"yes"
In Lua, all values except nil and false are truthy, so your if block will always run instead of your elseif block.
As said in the comments, accept:upper()=="YES" will fix it. accept:upper() returns a string where all the letters of accept are converted to upper case, so then you only have to compare it to one value.
Try this..
io.write("ENTER ANY STORY :-D ")
story=io.read()
io.write("\t OKAY!, THAT'S NICE :-D ")
io.write("\t DO YOU WANT TO REPLACE ANY TEXT:? ")
accept=io.read()
if accept=="YES" or accept == "yes" then
io.write("\t WHICH TEXT TO REPLAE? ")
replace=io.read()
--HERE IS THE REPLACING TEXT
io.write("\t WITH WHAT:? ")
with=io.read()
result=string.gsub(story,replace,with)
print("\t THE REPLACED TEXT IS: ",result)
elseif accept=="NO" or accept == "no" then
print(result)
end

Code stops working after first run

Hello stack overflow users, I have some code here:
local input = nil
print("What file do you want to access?")
input = io.read();
local file = io.open(input, "r")
function infiniteLoop()
print("What do you want to know more about?")
input = io.read();
while true do
line = file:read()
if line == nil then break end
if string.find(line, input) then
print(line)
end
end
end
repeat
infiniteLoop()
until false
As you could guess from the title, it works the first run, but the second time it won't print out what you want, regardless of it being in the file.
Example here
You need reset file pointer. try add file:seek(0) in the begining of function

Lua "or" statement issue

I'm very new to Lua, and I'm doing a very simple text based adventure thing, but it wont work. My code is as follows:
while input ~= ("leave cave" or "leave") do
print("What do you want to do?")
input = io.read()
if input == "inspect" then
print("You are in a cave")
elseif input == "leave cave" or "leave" then
print("You leave the cave")
elseif input == "inv" then
for i,v in pairs(inv) do
print(i, v)
end
else
print("You didn't write a valid command...")
end
end
-- leave cave
input = ""
print("What do you want to do?")
input = io.read()
while input ~= "follow path" do
if input == "inspect" then
print("You are at the base of a hill. There is a path.")
elseif input == "follow path" then
print("You follow the path. There is a gate.")
elseif input == "inv" then
for i,v in pairs(inv) do
print(v)
end
else
print("That's not a valid command...")
end
end
What I'm trying to do is have it so whenever the user types leave, or leave cave, it proceeds to the next segment (the path one), however, when I type "leave" and then type "inspect" again it says "I am in a cave" rather than what it should be saying which is saying that you left, and you see a path. And when I type leave cave, and then inspect, it spams "You are at the base of a hill. THERE IS A PATH" over and over, indefinitely.
And when I type "inv" it doesn't print my inventory, and instead prints "You left the cave," but doesn't actually leave.
a or b can't make a value that means "either a or b" -- that would be too complicated.
In fact, if you ask it to choose between two strings, it will just pick the first:
print("leave cave" or "leave") --> leave cave
or is only meant to be used on booleans -- you have to combine it on multiple full conditions:
while (input ~= "leave cave") and (input ~= "leave") do
In this case, a repeat ....... until <condition> loop might serve you better:
repeat
print("What do you want to do?")
input = io.read()
-- <do stuff>
until input == "leave" or input == "leave cave"
While or cannot accomplish such a complex operation, it is possible to recreate the effect yourself with some hacky metatable code.
Please note I do not reccomend using this code in any serious professional or commercial programs, or really at all for that matter, this code is inefficient and unecessary, however it is a fun piece of code to do exactly what you're looking for. It's just a fun way to experiment with the power of Lua.
local iseither
iseither = setmetatable({},{
__sub = function(arg1,arg2)
if arg2 == iseither then
arg2.Value = arg1
return arg2
else
if type(arg2) ~= "table" then
error("Second operator is -iseither- was not a table",2)
else
for i,v in ipairs(arg2) do
if arg1.Value == v then
arg1.Value = nil
return true
end
end
arg1.Value = nil
return false
end
end
end
})
print(1 -iseither- {1,2,3,4,5})

if statement not working in Lua for io.read

I'm trying to make a 'simple' Y/N answer choice thing. (That you saw all the time on old programs) But an If Statement I'm using doesn't seem to want to work. I even print out the variable and it is nowhere near what i want to compare yet it still passes it.
--Porgram Functions
function check()
--Local Variables
local num = 0
local loop = true
io.write("Continue? (Y/N):")
--User input
local input = io.read()
while(loop==true) do
if (input=="y" or "Y") then
print("Ok!")
loop = true
num = 1
elseif (input=="n" or "N") then
print("Fine...")
num = 2
else
print("Invalid Answser!")
loop = true
num = 0
end
end
print(input)
return(num)
end
print (check())
I would've written your function like this:
function check()
io.write("Continue? (Y/N): ")
answer = io.read()
while( not (answer == "Y" or answer == "N") ) do
io.write("Invalid Answer! Try again (Y/N): ")
answer = io.read()
end
if answer == "Y" then
print("Ok!")
return 1
else
print("Fine...")
return 2
end
end
print(check())
Some examples of its use:
Continue? (Y/N): Huh?
Invalid Answer! Try again (Y/N): N
Fine...
2
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "a.lua"
Continue? (Y/N): Huh?
Invalid Answer! Try again (Y/N): Y
Ok!
1
A working version of your code would be:
function check()
local num = 0
local loop = true
io.write("Continue? (Y/N):")
while(loop==true) do
--User input
local input = io.read()
if (input == "y" or input == "Y") then
print("Ok!")
num = 1
loop = false --we want to stop looping if input is valid
elseif (input == "n" or input == "N") then
print("Fine...")
num = 2
loop = false --we want to stop looping if input is valid
else
print("Invalid Answser!")
-- loop = true no need to set looping to true again
num = 0
end
end
return(num)
end
The changes made were:
Get the user input inside the while loop, this way if the input is invalid and the loop goes again the same logic behind getting the input is used, we don't have to code two cases for getting input; one outside the loop the other within. It also pauses execution when the loop starts again, this was what was producing all that output!
input == "y" or "Y" doesn't do what you think. Instead it evaluates to (input == "y") or ("Y"), what you want it input == "y" or input == "Y".
You needed to set loop to false when the input was either "y" or "Y" or "n" or "N", otherwise the loop would continue.
Fourthly setting the loop to true inside the loop is unnecessary, it begins as true, and the only change you can make is to set it to false. And since each of the conditions are mutually exclusive i.e input being "y" or "Y" mutually exclusive to input being "n" or "N" or it being neither "y" or "Y" or "n" or "N". You don't need to worry about it being set to false unless you wanted the loop to end.
local function check()
io.write"Continue? (Y/N): "
local ans, num = {y = 1, n = 2}
repeat
num = ans[io.read():lower()] or 3
io.write(({"Ok!\n","Fine...\n","Invalid Answer! Try again (Y/N): "})[num])
until num < 3
return num
end
print (check())

Resources