How to use string as equation in Roblox Studio? - lua

So,I have a string, let's say - 1+2-1+9-3 and I need to use it as equation and need to change text to result of this equation. How can i make it? I already did adding numbers to string, so i only need to get result of equation.

If you want to parse the string as a line of code, use loadstring(). This is basically the equivalent to JavaScript's eval(). Just keep in mind you have to allow loadstring to be used on your experience by clicking on ServerScriptService and checking the LoadStringEnabled box under Behaviour.
Example
Code
local parsecode = "1+2-1+9-3"
local parseres = loadstring("return "..parsecode)()
print(parseres)
Code Result
8 - Server - Script:3

Related

Computing a label from a label and relative offset

I have a macro that is generating two rules to avoid circularity issues. For a call like yaspl_bootstrap_library(name=foo, deps=[":bar"]) I want to generate the following rules:
yaspl_library(name=foo, deps=[":bar"])
yaspl_srcs(name=foo_srcs, deps=[":bar_srcs"])
Thus I need a function to turn ":bar" into ":bar_srcs". And while the obvious string concatenation works in this example it fails in the case where "//lib/foo" needs to be turned into "//lib/foo:foo_srcs".
This seems like a common thing that would happen in macros yet I cannot seem to find anything that does it easily.
First, I'll point out that this kind of string manipulation will not work with the select function (https://docs.bazel.build/versions/master/be/functions.html#select).
If it's not an issue for you, you can go ahead. This function can be written in a .bzl file. I agree this label manipulation functions should become available. In the meantime, you can try this function:
def explicit_label(label):
if ":" in label or "//" not in label:
return label
return label + ":" + label[label.rfind("/")+1:]
explicit_label(dep) + "_srcs"

Lua heredoc with variable interpolation?

Is there a way to use a heredoc type of notation in Lua that references variables within the string?
The basic idea of what I'm trying to do would look something like follows. The heredoc piece is fine, but within Lua you can't actually reference the variable in the manner I'm showing below.
username = "bigtunacan"
sql=[=[
SELECT * FROM users WHERE username='$bigtunacan';
]=]
There's no built-in string interpolation, but it can be trivially implemented with gsub and replacement table.
sql=[=[
SELECT * FROM users WHERE username='$username';
]=]
print((sql:gsub('$(%w+)', { username = 'bigtucan' })))
-- SELECT * FROM users WHERE username='bigtucan';
Note an extra set of () - this is so only first return - the interpolated string is used from gsub and the 2nd - number of replacements made - silently discarded. This might be important if you use result of gsub as last in list of arguments to some function where adding one more argument might produce different behavior.
Also if you want to use this in SQL context, you really should use placeholders instead.
There is no Lua construct that allows variable interpolation within any string. See Literal Strings in the official reference guide.
You could of course write a function that would parse it and do the substitutions.

Objective C: How to extract part of a String (e.g. start with 'src=')

I have a string as shown below
NSString *imagesource=#"<img src="http://edge.shop.com/edge.shop.com/ccimg.shop.com/250000/255300/255316/products/1142702303.jpg" title="Marley Coffee&reg; Mystic Morning Organic Ground Coffee" alt="Marley Coffee&reg;";
How can I select only the text starting with 'src=' (and ends with a space), in this case the source of the image
You can use a regular expression, check out this question which explains how to do it.
How to write regular expressions in Objective C (NSRegularExpression)?

Lua print on the same line

In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we have something similar to write of Pascal? How can consecutive print commands send their output to the same line?
print("Hello")
print("World")
Output:
Hello
world
I want to have this:
Hello world
Use io.write instead print, which is meant for simple uses, like debugging, anyway.
Expanding on lhf's correct answer, the io library is preferred for production use.
The print function in the base library is implemented as a primitive capability. It allows for quick and dirty scripts that compute something and print an answer, with little control over its presentation. Its principle benefits are that it coerces all arguments to string and that it separates each argument in the output with tabs and supplies a newline.
Those advantages quickly become defects when detailed control of the output is required. For that, you really need to use io.write. If you mix print and io.write in the same program, you might trip over another defect. print uses the C stdout file handle explicitly. This means that if you use io.output to change the output file handle, io.write will do what you expect but print won't.
A good compromise can be to implement a replacement for print in terms of io.write. It could look as simple as this untested sample where I've tried to write clearly rather than optimally and still handle nil arguments "correctly":
local write = io.write
function print(...)
local n = select("#",...)
for i = 1,n do
local v = tostring(select(i,...))
write(v)
if i~=n then write'\t' end
end
write'\n'
end
Once you are implementing your own version of print, then it can be tempting to improve it in other ways for your application. Using something with more formatting control than offered by tostring() is one good idea. Another is considering a separator other than a tab character.
As an alternative, just build up your string then write it out with a single print
You may not always have access to the io library.
You could use variables for "Hello" and "World". Then concatenate them later. Like this:
local h = "Hello"
local w = "World"
print(h..w)
It will be display, in this case, as "HelloWorld". But that's easy to fix. Hope this helped!
Adding on to #Searous's answer, try the following.
local h = "hello"
local w = "world"
print(h.." "..w)
You can concatenate both together, just concatenate a space between both variables.
local h = "Hello"
local w = "World!"
print(h, w)

TeX edef macro blues

I spent some time trying to write a 'helper' macro to test a parameter for a new value, else use the existing value -- default values exist for all parameter positions.
I wanted to be able to write:
\foo{left}{nil}{}{20pt}
so that the second parameter would used its current value but the third value would be the value empty string. I wanted to use the notation:
\edef\pA{\isnil{#1}{\pA){#1}} % one for each parameter
I defined \isnil like so:
\def\nil{nil}
\def\isnil#1#2#3{%
\edef\nilTest{#1}%
\ifx\nilTest\nil#2\else#3\fi
}
but when I tried to run it, TeX complained that \nilTest is an undefined control sequence.
That is true of course, but I want \pA to hold a value, not a recipe for a value, so it must be an \edef which means that all the macro test will be expanded but while will the \edef not protect the \nilTest -- is this a place to use \noexpand -- that did not seem to work for me.
EDIT: no digits in \cs names (yeah, I knew that.)
Why doesn't your solution work? \edef\pA{\isnil{#1}{\pA){#1}} expands \isnil and gets \edef\nilTest{.... Now \edef is not expandable and falls into a sequence of \pA as the first element. An attempt to expand the next macro \nilTest fails.
Use \setpar from the following code to change your parameter.
\def\nil{nil}
\def\setpar#1#2{%
\edef\nilTest{#2}%
\ifx\nilTest\nil\else\let#1\nilTest\fi}
\def\first{old first}
\def\second{old second}
\setpar \first{nil}
\setpar \second{new}
first = ``\first'', second = ``\second''
P.S. Do not use digits in your macro.

Resources