please tell me an analogue of a C ++ function but in lua
replaceAll("[^\\dA-Za-z]", "")
You can use Lua's patterns for that (which are not to be confused with regular expressions), in combination with string.gsub.
In your case, it's probably something like
local sanitized = raw:gsub("[^%dA-Za-z]", "")
(I don't have access to a Lua REPL at the moment, so this code is untested, but the links to the documentation should help you if in doubt)
Related
(Sorry for my broken English)
I'm trying to match an or in a string and if it is not enclosed in single quotes and replace it with a minus sign (-).
For example:
local input1 = "'condition1' or 'condition2'"
input1:gsub(pattern, "-") --> Should return "'condition1' - 'condition2'"
local input2 = "'condition1 or condition2'" -- Note the position of the '
input2:gsub(pattern, "-") --> Should return "'condition1 or condition2'"
Where pattern is the Lua pattern I am asking for.
Im sure that I have to use %b'' in order to detect if the or is quoted, so I tried this as my pattern: [^%b'']or
But that doesn't work for me.
Please note that I can use only pure Lua libraries (so no LPeg) as the code will be runned in different Lua runtimes (all 5.2) not supporting C libraries.
And please note that this question is not a duplicate - there is no question asking how to do this in Lua with its own patterns.
Try input:gsub("('.-'.-)or","%1-").
This assumes that or always appears after a quoted string. It captures everything from the quoted string until just before or and replaces this with the captured text followed by -, as required.
There's a common idiom for traversing a string whose characters may be escaped with a backslash by using the regex (\\.|.), like this:
alert( "some\\astring".replace(/(\\.|.)/g, "[$1]") )
That's in JavaScript. This code changes the string some\astring to [s][o][m][e][\a][s][t][r][i][n][g].
I know that Lua patterns don't support the OR operator, so we can't translate this regular expression directly into a Lua pattern.
Yet, I was wondering: is there an alternative way to do this (traversing possibly-escaped characters) in Lua, using a Lua pattern?
You can try
(\\?.)
and replace with [$1]
See it on Regexr.
? is a shortcut quantifier for 0 or 1 occurences, so the above pattern is matching a character and an optional backslash before. If ? is not working (I don't know lua) you can try {0,1} instead. That is the long version of the same.
Question is in the title, really. I saw someone use this earlier and I didn't know what the ! was used for.
local lowestIndex = 0;
local lowestValue = false;
for k, v in ipairs(playerElement) do
if !lowestValue or v.value < lowestValue then
lowestIndex = k;
lowestValue = v;
end
end
As others have said, ! normally has no function in Lua, and the code you posted would not normally be valid. However, it's quite trivial to extend Lua's parser to allow for custom syntax, and it's not unheard of for projects which embed Lua to add "more familiar" C-style syntax such as !var and != in addition to not var and ~=. One notable project which does this is Garry's Mod, and I'm sure there are others.
Of course, using custom syntax when the normal syntax is available (or customising it in the first place) is best avoided, if possible, to avoid exactly this sort of confusion.
It's a syntax error.
Some languages, mostly C and its relatives, use ! as a logical "not" operator, but Lua uses the not keyword instead, and does not use ! for anything as far as I know (not even as part of the inequality operator; it uses ~= where C uses !=).
You appear to have gotten hold of some Lua code written by someone who doesn't know that.
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)
I'd like to know if all formatting rules of printf functions currently work (or are implemented) in F# ?
For instance, if I want to align arguments on 9 characters (padding with spaces or 0), I would use:
printfn "%9A %9A" arg1 arg2 //don't seem to work
Thanks!
Do check out the docs
http://msdn.microsoft.com/en-us/library/ee370560(v=VS.100).aspx
(and possibly also these
http://en.wikibooks.org/wiki/F_Sharp_Programming/Input_and_Output
http://blogs.msdn.com/dsyme/archive/2010/01/08/some-tips-and-tricks-for-formatting-data-in-f-interactive-and-a-in-sprintf-printf-fprintf.aspx
)
though I am unclear about the fine points of the spec and implementation, especially regarding the %A specifier, which does various magical things. I'll see what other info I can dig up right now...
Read the documentation:
http://msdn.microsoft.com/en-us/library/ee370560.aspx