The full code itself is very convoluted, long, and novice-written; I'm trying to send x and z coordinates via rednet from a computer to a separate receiver turtle.
Receiver
rednet.broadcast("Awaiting Input!")
xAxis = rednet.receive() --Have tried tonumber(rednet.receive()) on both same result
zAxis = rednet.receive()
rednet.broadcast(xAxis) --Both values return 2 regardless of what I enter in the sender.
rednet.broadcast(zAxis)
sender
if(irrelevant == "genericstringhere") then
print(rednet.recieve()) --Awaiting Input!
io.write("X Axis: ") --Pizzaz
message = io.read() --Have tried using terms like X and xAxis.
rednet.broadcast(message) --Broadcast whatever tf I typed in.
sleep(0.3)
io.write("Z Axis: ") --More Pizzaz
message = io.read() --Have tried using terms like Z or zAxis
rednet.broadcast(message) --Broadcast whatever tf I typed in. again.
print(rednet.receive()) --Receive xAxis value from sender.
print(rednet.receive()) --Receive zAxis value from sender.
end
Both results at the end of execution return 2 instead of the value I input in io.read(). I have tried tonumber(), tostring(), and every combination of the two, and I can't seem to get it to work.
Per the documentation for rednet.receive:
Returns number senderID, any message, string protocol
The 2 you're seeing is the sender ID, not the message. Instead of xAxis = rednet.receive(), do senderId, xAxis = rednet.receive(), and similarly for everywhere else you assign its value. (If you were wondering why print showed you numbers before the messages, that's why.)
Related
The error I get:
query.lua:25: attempt to compare number with string
The rest of my code - I'm not sure why its not working, because I made sure that both values were in fact numbers, and the error still keeps happening, I have tried looking it up, and I cant find a solution, any help would be greatly appreciated.
print("Enter L,W,H values")
x = read()
y = read()
z = read()
length = 5
width = 5
height = 5
length = x
width = y
height = z
volume = length * width * height
print(volume.." blocks to mine")
turtle.refuel()
turtleFuel = turtle.getFuelLevel()
fuelNeeded = turtleFuel - volume
if fuelNeeded >= 0 then
print("Enough Fuel Detected")
else
print("not Enough fuel, error,"..fuelNeeded..", fuel required "..volume)
end
length1 = length
while length1 > 0 do
turtle.forward()
length1 = length1 - 1
end
In Computercraft, read() returns a string. You should change the first few lines of your code:
Instead of
x = read()
y = read()
z = read()
write
x = tonumber(read())
y = tonumber(read())
z = tonumber(read())
Do note, that read() is not the same as io.read(). One is a standard Lua function (which is also implemented in Computercraft) and read() is a function created specifically for CC computers and that can take a few extra parameters, and that you can use to do a few tricks once you get more advanced. (I am saying this because someone in the comments mentioned checking io.read())
Also, you mentioned that you "checked the type of the variable in the editor". Lua is a dynamically typed language, and the type of a variable CANNOT be known before the program is actually run. What your editor is probably doing is guessing the type of the variable based what it expects your program to do. These guesses are almost always inaccurate. That's why you should understand what your program is doing, and not rely on your editor. If you want to be extra sure, you want to run some tests, or the type of the variable simply is not known before the program is actually executed (because it may vary), you can use type(), like you have, to check its type at runtime. type() returns a string, representing the type of the variable:
type(nil) ---> nil
type("helloworld") ---> "string"
type(42) ---> "number"
type(function()print("HELLO")end) ---> "function"
type({1,2,3}) ---> "table"
I am working on a project where I need to find the integer value substituted from a character which is either 'K' 'M' or 'B'.
I am trying to find the best way for a user to input a string such as "1k" "12k" "100k" and to receive the value back in the appropriate way. Such as a user entering "12k" and I would receive "12000".
I am new to Lua, and I am not that great at string patterns.
if (string.match(text, 'k')) then
print(text)
local test = string.match(text, '%d+')
print(test)
end
local text = "1k"
print(tonumber((text:lower():gsub("[kmb]", {k="e3"}))))
I don't know what factors you use for M and B. What is that supposed to be? Million and billion?
I suggest you use the international standard. kilo (k), mega (M), giga (G) instead.
Then it would look like so:
local text = "1k"
print(tonumber((text:gsub("[mkMG]", {m = "e-3", k="e3", M="e6", G="e9"})))) --and so on
You can match the pattern as something like (%d+)([kmb]) to get the number and the suffix separately. Then just tonumber the first part and map the latter to a factor (using a table, for example) and multiply it with your result.
local factors = { k=1e3, m=1e6, --[and so on]] }
local num, suffix = string.match(text, '(%d+)([kmb])')
local result = tonumber(num) * factors[suffix]
I want to find specified text within a string in a given column, and count how many times that string is repeated throughout the entire column.
For example, Find "XX" within a string in a column and print to dialogue box the number of times that text was found.
Module m = current
Object o
string s
string x
int offset = null
int len = null
int c
for o in m do
{
string s = probeAttr_(o, "AttributeA")
x = o."Object Text" ""
if(findPlainText(s, "XX", offset, len, false)){
print "Success "
} else {
print "Failed to match"
}
}
I have tried to use command findPlainText but I am inadvertently passing every object as true.
As well I placed the output to print 'success' or 'Failed to match' so I can at least get a number count of what is being passed. Unfortunately it seems like everything is being passed!
My understanding is that 'probeAttr_(o, "AttributeA")' allows me to specify and enter what column to search. As well o."Object Text" "" now allows me to look within any object and search for any text contained. I also realize that variable x is not being used but assume it has some way of being used to solve this issue.
I only use DOORS at a surface level but having this ability will save other staff tons of time. I realize this may be accomplished using the DOORS advanced filtering capability but I'd be able to compound this code with other simple commands to save time.
Thank you in advance for your help!!
If you want to count every occurence of a specified string in a text in an attribute for all objects, I think Mike's proposal is the correct answer. If you are only interested, if the specified string occurs once in that object's attribute, I suggest using Regexp, as I find it very fast, quite powerful and nevertheless easy to use, e.g.:
Regexp reSearch = regexp2 "XX"
int iCounter = 0
string strOT = ""
for o in m do {
strOT = o."Object Text" ""
if (reSearch strOT) {
iCounter++
}
}
print "Counted: '" iCounter "'\n"
Most of this has been answered in (DXL/Doors) How to find and count a text in a string?
You can easily exchange the "print" with a counter.
I have an input form where I want to insert some numbers and calculate some results. So my input field looks like
<fmt:parseNumber var = "a" type = "number"
value = "${object.someAttribute}" integerOnly = "true"/>
<input type="number" name="someAttribute" required pattern="[0-9]" value="${a}" />
I want to do following: at first visit, a user should insert a number (Integer). In the calculation, all values are Double to prevent casting side effects. When the site is refreshed / the user wants to repeat the calculation, the input field should be preset with the recently used value. Therefore I tried fmt:parseNumber to parse the Double value from the object to an Integer.
At first try I omitted integerOnly = "true" but got an Error (as '1000.0' is not a valid input, that's understandable as I specified pattern="[0-9]").
But with integerOnly set, it changes the value from 1000.0 to 10000. What am I doing wrong? How could I parse it to achieve my goal?
If you are using EL 2.2+, you can simply convert a double to integer by invoking a non-getter method on your Double object:
${yourDouble.intValue()}
See:
https://stackoverflow.com/tags/el/info
If a user enters an invalid command then it returns the closest string to the user's input.
source = {"foo", "foo1"}
Suggest = function (input, source)
--do stuff
end
Thus when the user enters an undefined command "fo", for example it returns "foo".
Sorry if this is poorly explained, in a hurry.
You can use Levenshtein or Damerau–Levenshtein distance function to find commands closest to input. Here is a C library implementing both of these functions. Here is Levenshtein distance implemented in Lua.
The following function assumes distance is the distance function of choice. It takes optional radius argument to specify maximum distance from input to suggestion(you may not want to provide any suggestion if user entered something very strange, far from any existing commands). It returns an array of suggestions.
function suggest(input, source, radius)
local result = {}
local min_distance = radius or math.huge
for _, command in ipairs(source) do
local current_distance = distance(input, command)
if current_distance < min_distance then
min_distance = current_distance
result = {command}
elseif current_distance == min_distance then
table.insert(result, command)
end
end
return result
end
Note that this search algorithm is quite inefficient. It may or may not be a problem for you depending on number of commands and rate of searches.