How to print on same line using two print statement in lua - 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.

Related

Split a string on new lines, but include empty lines

Let's say I have a string with the contents
local my_str = [[
line1
line2
line4
]]
I'd like to get the following table:
{"line1","line2","","line4"}
In other words, I'd like the blank line 3 to be included in my result. I've tried the following:
local result = {};
for line in string.gmatch(my_str, "[^\n]+") do
table.insert(result, line);
end
However, this produces a result which will not include the blank line 3.
How can I make sure the blank line is included? Am I just using the wrong regex?
Try this instead:
local result = {};
for line in string.gmatch(my_str .. "\n", "(.-)\n") do
table.insert(result, line);
end
If you don't want the empty fifth element that gives you, then get rid of the blank line at the end of my_str, like this:
local my_str = [[
line1
line2
line4]]
(Note that a newline at the beginning of a long literal is ignored, but a newline at the end is not.)
You can replace the + with *, but that won't work in all Lua versions; LuaJIT will add random empty strings to your result (which isn't even technically wrong).
If your string always includes a newline character at the end of the last line like in your example, you can just do something like "([^\n]*)\n" to prevent random empty strings and the last empty string.
In Lua 5.2+ you can also just use a frontier pattern to check for either a newline or the end of the string: [^\n]*%f[\n\0], but that won't work in LuaJIT either.
If you need to support LuaJIT and don't have the trailing newline in your actual string, then you could just add it manually:
string.gmatch(my_str .. "\n", "([^\n]*)\n")

Splitting a long string at every / in lua

So I am making a function in lua where i wan't it to split a string at every "/" so for example:
local s = "Hello/GoodBye/Hi"
then i want it to split and input it to a table so it will look something like this:
Hello
GoodBye
Hi
This is my attempt but it doesn't really work:
local STR = "Hello/GoodBye/Hi"
strings = {}
for q,string in STR:gmatch("([/])(.-)%1") do
table.insert(strings, string)
end
That just returns:
Hello
Hi
That just returns:
Hello
Hi
When I run your code there is only a single element in strings which is "GoodBye".
The easiest way is to match anything that is not a / as in "[^/]+".
Also naming a variable string is not a good idea. You're shadowing Lua`s string library.

Lua: Remove a character from string

How would i for an example remove the h from "helloh" so it will look like "ello"
I have no idea what else to write but it looks like i need to write some more text and maybe add some code so this is just junk.
print("junk 1")
print("junk 2")
print("junk 4")
You can used string.gsub to replace characters, gsub stands for global subtitusion.
print(("helloh"):gsub("h", "")) -- replace all instances of `h` with empty string

How would I convert a string into a table?

I have been trying to convert a string into a table for example:
local stringtable = "{{"user123","Banned for cheating"},{"user124","Banned for making alt accounts"}}"
Code:
local table = "{{"user123","Banned for cheating"},{"user124","Banned for making alt accounts"}}"
print(table[1])
Output result:
Line 3: nil
Is there any sort of method of converting a string into a table? If so, let me know.
First of all, your Lua code will not work. You cannot have unescaped double quotes in a string delimited by double quotes. Use single quotes(') within a "-string, " within '...' or use heredoc syntax to be able to use both types of quotes, as shall I in the example below.
Secondly, your task cannot be solved with a regular expression, unless your table structure is very rigid; and even then Lua patterns will not be enough: you will need to use Perl-compatible regular expressions from Lua lrexlib library.
Thirdly, fortunately, Lua has a Lua interpreter available at runtime: the function loadstring. It returns a function that executes Lua code in its argument string. You just need to prepend return to your table code and call the returned function.
The code:
local stringtable = [===[
{{"user123","Banned for cheating"},{"user124","Banned for making alt accounts"}}
]===]
local tbl_func = loadstring ('return ' .. stringtable)
-- If stringtable is not valid Lua code, tbl_func will be nil:
local tbl = tbl_func and tbl_func() or nil
-- Test:
if tbl then
for _, user in ipairs (tbl) do
print (user[1] .. ': ' .. user[2])
end
else
print 'Could not compile stringtable'
end

Lua - How to print 2 things in one print statement

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

Resources