This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Adding strings together
Started by MrabEzreb, 24 December 2013 - 07:56 PMPosted 24 December 2013 - 08:56 PM
I am sure this is on the wiki somewhere, but it is not (at least not obviously) in the obvious place: the String API. I am creating a syllogism diagrammer for those who know what that is, and basicly I need to add letters together, for example, "A" + "A" = "AA". is there any easier way to do this than chaining together a whole bunch of if's and elseif's? Thank you!
Posted 24 December 2013 - 09:04 PM
You can do this:
local string1 = "a"
local string2 = "b"
local string3 = a..b --# concatenation operator is ..
print(string3) --# prints "ab" without quotes
It's in the PIL.Edited on 24 December 2013 - 08:05 PM
Posted 24 December 2013 - 10:20 PM
Did you mean to use string1 and string2 in you concatenation example? The code you've got there doesn't make any sense, since a and b would both be nil.
Posted 25 December 2013 - 02:45 AM
Here's how you do it:
MyString1 = "fu"
MyString2 = " manchu"
MyString3 = MyString1..Mystring2
The ".." concatenates strings, i.e. sticks them together into a new string.Posted 25 December 2013 - 08:28 AM
DUH! thank you!
(I be retard)
(I be retard)
Posted 25 December 2013 - 08:50 AM
Okay, this I might not need but might be helpful. If "A".."A" = "AA", how do I make "AA" - "A" = "A"?
Posted 25 December 2013 - 09:38 AM
Yeah, it was early in the morning. Thanks.Did you mean to use string1 and string2 in you concatenation example? The code you've got there doesn't make any sense, since a and b would both be nil.
I'm not sure if there's a specific operator for that. You might find something in the string library (if you know specifically it will be "AA", you can use string.sub), but if not, you can totally do this in Lua code.Okay, this I might not need but might be helpful. If "A".."A" = "AA", how do I make "AA" - "A" = "A"?
Posted 25 December 2013 - 09:53 AM
so you are saying that A: I need to know every "equation" beforehand and B: the easiest way to do this is with a lot of if's and elseif's?
Posted 25 December 2013 - 09:54 AM
I am not stating that this is the best way. I am stating that it is a way.so you are saying that A: I need to know every "equation" beforehand and B: the easiest way to do this is with a lot of if's and elseif's?
Posted 25 December 2013 - 10:18 AM
You would have to edit the strings metatable, but that's not possible without editing the bios.Okay, this I might not need but might be helpful. If "A".."A" = "AA", how do I make "AA" - "A" = "A"?
Posted 25 December 2013 - 10:24 AM
So you probably couldn't do this on a server, unless you were an admin and has access to bios.luaYou would have to edit the strings metatable, but that's not possible without editing the bios.Okay, this I might not need but might be helpful. If "A".."A" = "AA", how do I make "AA" - "A" = "A"?
Posted 25 December 2013 - 11:22 AM
Use this as a reference: http://lua-users.org...LibraryTutorial
Anyway, to answer your question you need to use string.sub
Im not sure if this is the appropriate answer, in the case where you want to strip one character off: "AAB" - "A" = "AB"
Then I have made a function for you:
Anyway, to answer your question you need to use string.sub
local str = "AA"
--# lets get one A of!
local string = string.sub(str, 1, #str - 1)
Im not sure if this is the appropriate answer, in the case where you want to strip one character off: "AAB" - "A" = "AB"
Then I have made a function for you:
local function string.strip(self, str)
if type(str) ~= "string" then
error("'String expected, got " .. type(str), 2)
end
local newStr = ""
local stripped = true
for i = 1, self:len() do
if str == self:sub(i, i) and not stripped then
stripped = true
else
newStr = newStr .. self:sub(i, i)
end
end
return newStr
end
--#usage
local str = "AA"
str = str:strip("A")
Though, all of this is untested, should work thoughEdited on 25 December 2013 - 10:22 AM
Posted 25 December 2013 - 12:07 PM
Well, you can't declare local functions inside a table. And you'd have to modify the string metatable anyway.-snip-
Posted 25 December 2013 - 12:38 PM
local function string.strip(self, str) if type(str) ~= "string" then error("'String expected, got " .. type(str), 2) end local newStr = "" local stripped = true for i = 1, self:len() do if str == self:sub(i, i) and not stripped then stripped = true else newStr = newStr .. self:sub(i, i) end end return newStr end --#usage local str = "AA" str = str:strip("A")
So, let me see if I understand this confusing code…
local function string.strip(self, str) --#this defines a function, [s]I assume that it assigns the function ("strip" with the arguments of "self" and "str") to the "string" api. so that if for example I put this in the file "stripAPI" and then ran os.loadAPI("stripAPI"), I wouldn't type "stripAPI.strip()", I would type "string.strip()". also what type of arguments are these? "str" is a string (obvi), but what is "self"?[/s] scratch that, "string.strip(self, str)" is accually "(string = the string you are stripping from):strip([self = it tells the function that in "string.strip", the "string" is an argument], [str = what you are stripping off])
if type(str) ~= "string" then --# this determines if "str" is a string or not, if not it runs the "error()" function (I happen to want to figure out how the error() function works btw).
error("'String expected, got " .. type(str), 2)
end
local newStr = "" -- #defines a empty string to the variable "newStr".
local stripped = true --#defines "stripped" as "true".
for i = 1, self:len() do -- #will run the following code and set "i" to "1", and then run until "i" is equal to "self:len()". I know what "self" is (the "string" in "string:strip"), but what is ":len()"? from your use of "self" as an argument, "len()" is a function and "self:len()" is "len()" with "self" as its "self" argument. (the function for len() is defined as X.len(self)?)
if str == self:sub(i, i) and not stripped then --#will set "stripped" to "true" if A: "stripped" is false and B: "str" is equal to "self:sub(i, i)". You mentioned "sub()" in a previous post, what is it? I assume it is again a function with a "self" argument.
stripped = true
else --#if "str" is not equal to "self:sub(i, i)" and/or "stripped" is true, "newStr" is set to "newStr" + "self:sub(i, i)"
newStr = newStr .. self:sub(i, i)
end
end
return newStr --#once "i" is equal to "self:len()", the function returns the data contained in "newStr" to the program running.
end
--#usage
local str = "AA"
str = str:strip("A")
did I about get that right? please help me understand!Edited on 25 December 2013 - 11:42 AM
Posted 25 December 2013 - 03:37 PM
Been long since I used Lua, as you might know :P/>Well, you can't declare local functions inside a table. And you'd have to modify the string metatable anyway.-snip-
I would explain it, but I'm on a phone right now so it's a bit unhandy to explain.
As soon as I'm onto a computer I'm going to fix the code and try to explain it.
Posted 25 December 2013 - 04:21 PM
kk ty. want to see the code that I am making?Been long since I used Lua, as you might know :P/>Well, you can't declare local functions inside a table. And you'd have to modify the string metatable anyway.-snip-
I would explain it, but I'm on a phone right now so it's a bit unhandy to explain.
As soon as I'm onto a computer I'm going to fix the code and try to explain it.
Posted 25 December 2013 - 05:06 PM
Yes please
Posted 27 December 2013 - 01:50 PM
kk here it is, it is very large and many functions are not used.
accually it stoped saying that and is working (somewhat) now
vennMessage = "Not implemented, showing canvas"
os.loadAPI("button")
m = peripheral.wrap("top")
m.clear()
sBox = "Search"
modem = peripheral.wrap("right")
buttonSet = 1
modem.open(0)
modem.open(2)
peripheral.call("computer_6", "reboot")
m.setBackgroundColor(1)
m.setTextColor(32768)
function checkMoodFigure(l1, l2, l3, n1)
if l1 == "A" or l1 == "E" or l1 == "I" or l1 == "O" then
if l2 == "A" or l2 == "E" or l2 == "I" or l2 == "O" then
if l3 == "A" or l3 == "E" or l3 == "I" or l3 == "O" then
if n1 == 1 or n1 == 2 or n1 == 3 or n1 == 4 then
encryptRedNet("true", send, modem, 2, 1)
return true
else
encryptRedNet("false", send, modem, 2, 1)
return false
end
else
encryptRedNet("false", send, modem, 2, 1)
return false
end
else
encryptRedNet("false", send, modem, 2, 1)
return false
end
else
encryptRedNet("false", send, modem, 2, 1)
return false
end
end
function getSyllogism(letter1, number1)
if number1 == 1 then
if letter1 == "O" then
return "Some M are not P"
elseif letter1 == "A" then
return "All M are P"
elseif letter1 == "E" then
return "No M are P"
elseif letter1 == "I" then
return "Some M are P"
end
end
eof()--right here it says I am calling nil but if I change it to "end" it says eof expected
--if I put an "end" here it says eof expected as well
if number1 == 2 then
if letter1 == "O" then
letter1Str = "Some P are not M"
elseif letter1 == "A" then
letter1Str = "All P are M"
elseif letter1 == "E" then
letter1Str = "No P are M"
elseif letter1 == "I" then
letter1Str = "Some P are M"
end
end
eof()
eof()
if number1 == 3 then
if letter1 == "O" then
letter1Str = "Some M are not P"
elseif letter1 == "A" then
letter1Str = "All M are P"
elseif letter1 == "E" then
letter1Str = "No M are P"
elseif letter1 == "I" then
letter1Str = "Some M are P"
end
end
eof()
if number1 == 4 then
if letter1 == "O" then
letter1Str = "Some P are not M"
elseif letter1 == "A" then
letter1Str = "All P are M"
elseif letter1 == "E" then
letter1Str = "No P are M"
elseif letter1 == "I" then
letter1Str = "Some P are M"
end
end
eof()
eof()
function sBoxFix(sBoX)
if sBoX == nil then
return "Search"
else
return sBoX
end
end
function heading2(text, row)
w, h = m.getSize()
m.setCursorPos((w-string.len(text))/2+1, row)
m.write(text)
end
function encryptRedNet(message, sndRec, modem, to, from)
if sndRec == "send" then
encMessage = textutils.serialize(message)
modem.transmit(to, from, encMessage)
return true
elseif sndRec == "recive" then
decMessage = textutils.unserialize(message)
return decMessage
end
end
function fillTable()
button.setTable("A", a, 10,20,3,5)
button.setTable("E", e, 22,32,3,5)
button.setTable("I", i, 10,20,8,10)
button.setTable("O", o, 22,32,8,10)
button.screen()
m.setBackgroundColor(1)
m.setTextColor(32768)
end
function fillTable2()
button.unsetTable("A")
button.unsetTable("E")
button.unsetTable("I")
button.unsetTable("O")
button.setTable("figure1", f, 10,20,3,5)
button.setTable("figure2", g, 22,32,3,5)
button.setTable("figure3", h, 10,20,8,10)
button.setTable("figure4", j, 22,32,8,10)
button.screen()
m.setBackgroundColor(1)
m.setTextColor(32768)
end
function fillTable3()
button.unsetTable("figure1")
button.unsetTable("figure2")
button.unsetTable("figure3")
button.unsetTable("figure4")
button.setTable("Back", back, 10,20,8,10)
button.setTable("Generate", Venn, 22,32,8,10)
button.screen()
m.setBackgroundColor(1)
m.setTextColor(32768)
end
function unFillTable2()
button.unsetTable("figure1")
button.unsetTable("figure2")
button.unsetTable("figure3")
button.unsetTable("figure4")
m.setBackgroundColor(1)
m.setTextColor(32768)
end
function getClick()
event234,side,x,y = os.pullEvent("monitor_touch")
button.checkxy(x,y)
return event234
end
function a()
button.flash("A")
encryptRedNet("A", "send", modem, 0, 0)
end
function e()
button.flash("E")
encryptRedNet("E", "send", modem, 0, 0)
end
function i()
button.flash("I")
encryptRedNet("I", "send", modem, 0, 0)
end
function o()
button.flash("O")
encryptRedNet("O", "send", modem, 0, 0)
end
function f()
button.flash("figure1")
encryptRedNet("1", "send", modem, 0, 0)
end
function g()
button.flash("figure2")
encryptRedNet("2", "send", modem, 0, 0)
end
function h()
button.flash("figure3")
encryptRedNet("3", "send", modem, 0, 0)
end
function j()
button.flash("figure4")
encryptRedNet("4", "send", modem, 0, 0)
end
function back()
button.flash("Back")
os.reboot()
end
function Venn()
m.clear()
m.setBackgroundColor(1)
m.setTextColor(32768)
button.heading(vennMessage)
term.redirect(m)
VennDiagramGraphic = paintutils.loadImage("canvas")
paintutils.drawImage(VennDiagramGraphic, 1, 2)
m.setBackgroundColor(1)
m.setTextColor(32768)
term.restore()
m.setCursorPos(7, 9)
m.write("S")
m.setCursorPos(20, 9)
m.write("M")
m.setCursorPos(13, 14)
m.write("P")
while true do
event534 = os.pullEvent("monitor_touch")
if event534 == "monitor_touch" then
event534 = nil
os.reboot()
end
end
end
while true do
button.heading(sBox)
button.label(1,5,"Logic!")
if buttonSet == 1 then
m.clear()
fillTable()
button.heading(sBox)
button.label(1,5,"Logic!")
m.setBackgroundColor(1)
m.setTextColor(32768)
elseif buttonSet == 2 then
m.clear()
fillTable2()
button.heading(sBox)
button.label(1,5,"Logic!")
m.clear()
fillTable2()
button.heading(sBox)
button.label(1,5,"Logic!")
m.setBackgroundColor(1)
m.setTextColor(32768)
elseif buttonSet == 3 then
m.clear()
fillTable3()
button.heading(sBox)
button.label(1,5,"Logic!")
m.setBackgroundColor(1)
m.setTextColor(32768)
end
button.screen()
clickButton = getClick()
if clickButton == "monitor_touch" then
event234 = nil
event, side, chan, rchan, message, distance = os.pullEvent("modem_message")
sSBox = encryptRedNet(message, "recive", modem, 0, 0)
buttonSet = sSBox["bSet"]
if sSBox["type"] == "string" then
sBox = sSBox["sent"]
sSBox = nil
m.setBackgroundColor(1)
m.setTextColor(32768)
elseif sSBox["type"] == "number" then
m.clear()
fillTable2()
sBox = sSBox["sent"]
buttonSet = 2
sSBox = nil
m.setBackgroundColor(1)
m.setTextColor(32768)
elseif sSBox["type"] == "table" then
m.clear()
m.setBackgroundColor(1)
m.setTextColor(32768)
sBox = sSBox["letterMood"].." - "..sSBox["numberFigure"]
button.heading(sBox)
button.label(1,5,"Logic!")
heading2("Computing...", 2)
VennDiagram = sSBox["letterMood"]..sSBox["numberFigure"]
sSBox = nil
os.sleep(2)
--encryptRedNet("compute", "send", modem, 0, 0)
m.setBackgroundColor(1)
m.setTextColor(32768)
elseif sSBox["type"] == "compute" then
m.clear()
m.setBackgroundColor(1)
m.setTextColor(32768)
button.heading(sBox)
majPrem = sSBox["letterMood1"]
minPrem = sSBox["letterMood2"]
finCon = sSBox["letterMood3"]
numberFigure = sSBox["numberFigure"]
sBox = majPrem..minPrem..finCon.."-"..numberFigure
thisthat = {"gotThis!"}
encryptRedNet(thisthat, send, modem, 2, 1)
print(majPrem..minPrem..finCon..numberFigure)
button.heading(sBox)
button.label(1,5,"Logic!")
button.screen()
m.setBackgroundColor(1)
m.setTextColor(32768)
line2 = getSyllogism(majPrem, numberFigure)
print(line2)
line3 = getSyllogism(minPrem, numberFigure)
line4 = getSyllogism(finCon, numberFigure)
heading2(line2, 2)
--heading2(line3, 3)
--heading2(line4, 4)
end --Line 380
end
end
eof()
I am kindof confused about that eof()/end thing, what is the point of eof()?accually it stoped saying that and is working (somewhat) now
Posted 27 December 2013 - 02:29 PM
how do you determine if something is a string? is this right?
nvm, its
also, HOW DOES error() WORK?!
something = something
if type(something) == string then
print("YES")
else
print("no")
end
nvm, its
if type(something) == "string" then
...
also, HOW DOES error() WORK?!
Posted 27 December 2013 - 02:30 PM
Well that's close, But it's this
something = "Hello World!"
if type( something ) == "string" then -- Type returns a string and not a variable
print("It was a string")
else
print("Nope")
end
Edited on 27 December 2013 - 01:30 PM
Posted 27 December 2013 - 02:30 PM
right, I corrected myself, but how does "error()" work?!
Posted 27 December 2013 - 03:07 PM
its something like this:
what is the number?
error(string, number?)
what is the number?
Posted 27 December 2013 - 03:31 PM
Posted 27 December 2013 - 04:06 PM
ty
but it only says:
I am about to create a YT vid about this program, stay tuned!
but it only says:
function testError(level)
error("I am an error!", level)
end
testError(1) returns myAwesomeFile:4:
testError(2) returns testError:1:
what would testError(3) or testError(4) or (5) return?I am about to create a YT vid about this program, stay tuned!
Posted 27 December 2013 - 05:57 PM
http://turtlescripts.com/project/gjdhmw-Logic-Logic-A-Syllogism-Lookup-and-Diagrammer-
check it out
I linked the video to it
check it out
I linked the video to it