This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KevinW1998's profile picture

Replace line break into a space

Started by KevinW1998, 06 July 2012 - 03:12 PM
KevinW1998 #1
Posted 06 July 2012 - 05:12 PM
How do I replace line breaks into a space?
for examble using string.gsub()?

e.g.:
str1
str2
str3
into:
"str1 str2 str3"

Kevin
MysticT #2
Posted 06 July 2012 - 05:35 PM

string.gsub(yourString, "n", " ")
KevinW1998 #3
Posted 06 July 2012 - 07:09 PM
I've already tried this, but it doesn't work :P/>/>
Kolpa #4
Posted 06 July 2012 - 08:48 PM
wherer are u getting the string from ? if it its over http.get then use

string.gsub(yourString, "<br>", " ")
KevinW1998 #5
Posted 06 July 2012 - 09:23 PM
I'm getting the string from a file and a few special lines i'm saving in a string..
The problem is that the strings has the line breaks form h.readLine() too
so I want to convert the line breaks to a space caracter
MysticT #6
Posted 06 July 2012 - 10:10 PM
When you read the string from a file, using file.readLine, it deosn't append a new line at the end.
Post the code you use, so we can see what's wrong.
KevinW1998 #7
Posted 07 July 2012 - 06:35 AM
Mhh.. okay
Then textutils.serialize() might be the problem?
Anyway here is part of my code:

xread = h.readLine()
while xread ~= "version:" do
if xread == nil then
  errorStop()
  return
end
xstr = xstr.." "..xread
xread = h.readLine()
end
xprogramPack[2] = xstr

This works fine at the moment!
Because I'm using a table in a table (what in lua doesn't work) I need to covert my 2nd table in a string using textutils.serialize()
This string I'm copying in the main table…

If I using textutils.unserialize() I am getting the error: attempt to index ? (a nil value)

If I print the string without unserializing I see line breaks..
Where I've got my line breaks in my file I've got it in the table too.. :P/>/>
Kolpa #8
Posted 07 July 2012 - 09:39 AM
cant u do this to kinda store a table in a table ?

table1 ={}
table1.test1[1] = "i don't know"
table1.test2[1] = "me neither"
KevinW1998 #9
Posted 07 July 2012 - 12:52 PM
Nope! You'll get the error: index expected, got nil
MysticT #10
Posted 07 July 2012 - 06:21 PM
Are you sure there's line breaks? Cause if you use print or write, it adds them automaticaly to fit the screen, try with term.write.
Also,
Because I'm using a table in a table (what in lua doesn't work)
What do you mean that they don't work? You can do something like:

local t = {}
t.sub_table = {}
t["sub_table"][1] = "some value"
print(t.sub_table[1])
KevinW1998 #11
Posted 07 July 2012 - 07:50 PM
Yep thanks man ^^
The line breaks are gone!