Lua - How to print 2 things in one print statement - lua

In python you are able to print 2 things by using one statement by typing
print("Hello" + " World")
Output would be "Hello world"
So is there a simple to do this again in Lua?
I'm trying to make the statement print the percentage and the percentage sign. This is currently what I have
function update()
local hp = crysHu.Health/ crysHu.MaxHealth
local text = script.Parent.TextLabel
healthBar:TweenSize(UDim2.new(hp,0,1,0),"In","Linear",1)
text.Text = math.floor(hp*100)
end
text.Text = math.floor(hp*100) is the part that I need help with FYI.
doing text.Text = (math.floor(hp*100) + "%") doesn't work.

If you're doing simple string manipulations, you can concatenate them with .. like this :
local foo = 100
print( tostring(foo) .. "%") -- 100%
or if you want more specific formatting you can use string.format
local foo = 100
print( string.format("%d%%", foo)) -- 100%

Use a ,. Its the same for both Lua and Python though Lua puts a tab between them in a print:
print(2, 3) # 2 3
or use io.write but then you need to handle the newline.
io.write("hello", " world\n") # hello world

Related

How to print on same line using two print statement in lua

Example:
Input:
Print("hello")
Print("hi")
Output:
hello
hi
I want to output both "hello" and "hi" on the same line like hellohi without using concatenation.
You can achieve what you want using io.write(). This does not write a line break by default.
io.write("hello")
io.write("hi")
Output:
hellohi
To add line breaks simply add \n to your io.write e.g.
io.write("hello\nhi")
output:
hello
hi
Moreover, if you want to write a variable with a string:
Age = 18
print("Age: " .. Age)
where the .. concatenates them together.

How to remove last line from a string in Lua?

I am using Lua in World of Warcraft.
I have this string:
"This\nis\nmy\nlife."
So when printed, the output is this:
This
is
my
life.
How can I store the entire string except the last line in a new variable?
So I want the output of the new variable to be this:
This
is
my
I want the Lua code to find the last line (regardless of how many lines in the string), remove the last line and store the remaining lines in a new variable.
Thank you.
So I found that Egor Skriptunoff's solutions in the comments worked very well indeed but I am unable to mark his comments as an answer so I'll put his answers here.
This removes the last line and stores the remaining lines in a new variable:
new_str = old_str:gsub("\n[^\n]*$", "")
If there is a new line marker at the end of the last line, Egor posted this as a solution:
new_str = old_str:gsub("\n[^\n]*(\n?)$", "%1")
While this removes the first line and stores the remaining lines in a new variable:
first_line = old_str:match("[^\n]*")
Thanks for your help, Egor.
Most efficient solution is plain string.find.
local s = "This\nis\nmy\nlife." -- string with newlines
local s1 = "Thisismylife." -- string without newlines
local function RemoveLastLine(str)
local pos = 0 -- start position
while true do -- loop for searching newlines
local nl = string.find(str, "\n", pos, true) -- find next newline, true indicates we use plain search, this speeds up on LuaJIT.
if not nl then break end -- We didn't find any newline or no newlines left.
pos = nl + 1 -- Save newline position, + 1 is necessary to avoid infinite loop of scanning the same newline, so we search for newlines __after__ this character
end
if pos == 0 then return str end -- If didn't find any newline, return original string
return string.sub(str, 1, pos - 2) -- Return substring from the beginning of the string up to last newline (- 2 returns new string without the last newline itself
end
print(RemoveLastLine(s))
print(RemoveLastLine(s1))
Keep in mind this works only for strings with \n-style newlines, if you have \n\r or \r\n easier solution would be a pattern.
This solution is efficient for LuaJIT and for long strings.
For small strings string.sub(s1, 1, string.find(s1,"\n[^\n]*$") - 1) is fine (Not on LuaJIT tho).
I scan it backward because it more easier to remove thing from back with backward scanning rather than forward it would be more complex if you scan forward and much simpler scanning backward
I succeed it in one take
function removeLastLine(str) --It will return empty string when there just 1 line
local letters = {}
for let in string.gmatch(str, ".") do --Extract letter by letter to a table
table.insert(letters, let)
end
local i = #letters --We're scanning backward
while i >= 0 do --Scan from bacward
if letters[i] == "\n" then
letters[i] = nil
break
end
letters[i] = nil --Remove letter from letters table
i = i - 1
end
return table.concat(letters)
end
print("This\nis\nmy\nlife.")
print(removeLastLine("This\nis\nmy\nlife."))
How the code work
The letters in str argument will be extracted to a table ("Hello" will become {"H", "e", "l", "l", "o"})
i local is set to the end of the table because we scan it from the back to front
Check if letters[i] is \n if it newline then goto step 7
Remove entry at letters[i]
Minus i with 1
Goto step 3 until i is zero if i is zero then goto step 8
Remove entry at letters[i] because it havent removed when checking for newline
Return table.concat(letters). Won't cause error because table.concat return empty string if the table is empty
#! /usr/bin/env lua
local serif = "Is this the\nreal life?\nIs this\njust fantasy?"
local reversed = serif :reverse() -- flip it
local pos = reversed :find( '\n' ) +1 -- count backwards
local sans_serif = serif :sub( 1, -pos ) -- strip it
print( sans_serif )
you can oneline it if you want, same results.
local str = "Is this the\nreal life?\nIs this\njust fantasy?"
print( str :sub( 1, -str :reverse() :find( '\n' ) -1 ) )
Is this the
real life?
Is this

print () command and multiple items

Some context, This is some of my first programming in Lua and I am doing it with QSC software.
local a = Controls.Flippy.String
local b = Controls.Flippy.Value
local c = Controls.Flippy.Position
local d = " "
print(a..d..b..d..c)
This worked but is there away I can write strings in line with a variables. Such as:
print("controls "a" "b" "c)
Same way you put the strings together--with .., the concatenation operator.
print("controls "..a.." "..b.." "..c)
It is also possible to use print with multiple arguments, though the spacing is often different (which may or may not be desirable):
print("controls",a,b,c)
You can just write the strings as
print("controls", a, b, c)
You can choose not to put spaces in between if you wish not to.
Hope that helps!
There are several ways to print multiple values in a line.
local name = "Frank"
local age = 15
-- simple, but not applicable for my example due to formatting:
print("My name is", name, "I'm", age, "years old")
-- using the concatenation operator ..
print("My name is " .. name .. ". I'm " .. age .. " years old")
-- using string.format, best for complex expressions, capable of advanced formatting
print(string.format("My name is %s. I'm %d years old.", name, age))

Splitting strings in Lua

I'm very new to Lua, So sorry if I sound really stupid.
I'm trying to make a program that does something a bit like this:
User input: "Hello world"
Var1: Hello
Var2: world
Because I have no idea what I'm doing, All I have is test = io.read(), And I have no idea what to do next.
I appreciate any help!
Thanks, Morgan.
If you want split words, you can do so:
input = "Hello world"
-- declare a table to store the results
-- use tables instead of single variables, if you don't know how many results you'll have
t_result = {}
-- scan the input
for k in input:gmatch('(%w+)') do table.insert(t_result, k) end
-- input:gmatch('(%w+)')
-- with generic match function will the input scanned for matches by the given pattern
-- it's the same like: string.gmatch(input, '(%w+)')
-- meaning of the search pattern:
---- "%w" = word character
---- "+" = one or more times
---- "()" = capture the match and return it to the searching variable "k"
-- table.insert(t_result, k)
-- each captured occurence of search result will stored in the result table
-- output
for i=1, #t_result do print(t_result[i]) end
-- #t_result: with "#" you get the length of the table (it's not usable for each kind of tables)
-- other way:
-- for k in pairs(t_result) do print(t_result[k]) end
Output:
Hello
world

Lua: Quoted arguments passed as one in function

I'm attempting to simplify a script, and my attempts are failing. I'm making a function that will pass the given arguments and turn them into an indexed table, but I want to be able to pass quoted and non-quoted alike and have the function recognize that quoted arguments are considered one value while also respecting non-quoted arguments.
For example:
makelist dog "brown mouse" cat tiger "colorful parrot"
should return an indexed table like the following:
list_table = {"dog", "brown mouse", "cat", "tiger", "colorful parrot"}
The code I have works for quoted, but it's messing up on the non-quoted, and on top of that, adds the quoted arguments a second time. Here's what I have:
function makelist(str)
require 'tprint'
local list_table = {}
for word in string.gmatch(str, '%b""') do
table.insert(list_table, word)
end
for word in string.gmatch(str, '[^%p](%a+)[^%p]') do
table.insert(list_table, word)
end
tprint(list_table)
end
I'm not understanding why the omission of quotes is being ignored, and also is chopping off the first letter. That is, this is the output I receive from tprint (a function that prints a table out, not relevant to the code):
makelist('dog "brown mouse" cat tiger "colorful parrot"')
1=""brown mouse""
2=""colorful parrot""
3="og"
4="rown"
5="mouse"
6="cat"
7="tiger"
8="olorful"
9="parrot"
As you can see, 'd', 'b', and 'c' are missing. What fixes do I need to make so that I can get the following output instead?
1="brown mouse"
2="colorful parrot"
3="dog"
4="cat"
5="tiger"
Or better yet, have them retain the same order they were dictated as arguments, if that's possible at all.
local function makelist(str)
local t = {}
for quoted, non_quoted in ('""'..str):gmatch'(%b"")([^"]*)' do
table.insert(t, quoted ~= '""' and quoted:sub(2,-2) or nil)
for word in non_quoted:gmatch'%S+' do
table.insert(t, word)
end
end
return t
end
It may be easier to simply split on whitespaces and concatenate those elements that are inside quotes. Something like this may work (I added few more test cases):
function makelist(str)
local params, quoted = {}, false
for sep, word in str:gmatch("(%s*)(%S+)") do
local word, oquote = word:gsub('^"', "") -- check opening quote
local word, cquote = word:gsub('"$', "") -- check closing quote
-- flip open/close quotes when inside quoted string
if quoted then -- if already quoted, then concatenate
params[#params] = params[#params]..sep..word
else -- otherwise, add a new element to the list
params[#params+1] = word
end
if quoted and word == "" then oquote, cquote = 0, oquote end
quoted = (quoted or (oquote > 0)) and not (cquote > 0)
end
return params
end
local list = makelist([[
dog "brown mouse" cat tiger " colorful parrot " "quoted"
in"quoted "terminated by space " " space started" next "unbalanced
]])
for k, v in ipairs(list) do print(k, v) end
This prints the following list for me:
1 dog
2 brown mouse
3 cat
4 tiger
5 colorful parrot
6 quoted
7 in"quoted
8 terminated by space
9 space started
10 next
11 unbalanced
First thanks for your question, got me to learn the basics of Lua!
Second, so I think you went with your solution in a bit of misdirection. Looking at the question I just said why don't you split once by the quotes (") and than choose where you want to split by space.
This is what I came up with:
function makelist(str)
local list_table = {}
i=0
in_quotes = 1
if str:sub(0,1) == '"' then
in_quotes = 0
end
for section in string.gmatch(str, '[^"]+') do
i = i + 1
if (i % 2) == in_quotes then
for word in string.gmatch(section, '[^ ]+') do
table.insert(list_table, word)
end
else
table.insert(list_table, section)
end
end
for key,value in pairs(list_table) do print(key,value) end
end
The result:
1 dog
2 brown mouse
3 cat
4 tiger
5 colorful parrot

Resources