This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Take apart sections of strings.
Started by MrabEzreb, 16 April 2014 - 07:17 PMPosted 16 April 2014 - 09:17 PM
I am currently working on a program that uses input from a website (via a Google Drive spreadsheet created by Form+), and am trying to figure out how to take apart a line of a text file into each cell of the spreadsheet. Each cell has a "tab" character inbetween, but I do not think that CC recognizes that. I have instructed the users of the website to encase every answer in 'single quotes' so that I could find the beginning and end. (some of the cells will have more than one word in them, so I can't go by spaces. Also they are not always the same number of words.) Could anyone help me with this? Thanks!
Posted 16 April 2014 - 09:34 PM
Dumb question, but did you try to split the string at the tabs via the tab escape sequence \t?
Edited on 16 April 2014 - 07:34 PM
Posted 16 April 2014 - 09:40 PM
? escape sequence? i have no idea what u r talking about. :P/>
Posted 16 April 2014 - 10:34 PM
You may want to take a look at this: http://en.wikipedia....Escape_sequence and this: http://en.wikipedia....ntrol_character
Basically those sequences are a way to print non-printable characters like tabs, linebreaks and so on. \n is a common one and creates a new line. So for example, if you do
Use \t as delimiter to split you string into each cell.
Basically those sequences are a way to print non-printable characters like tabs, linebreaks and so on. \n is a common one and creates a new line. So for example, if you do
print(Helloe\nWorld!)
you would get
Hello
World!
as output. Similar thing with \t, with the difference that there would be a tabulator between "Hello" and "World!".Use \t as delimiter to split you string into each cell.
Posted 16 April 2014 - 11:49 PM
so… how would I use that?
Posted 17 April 2014 - 12:17 AM
Disclaimer:
He literally just told you.
Take your string and add the /t escape to it.
Spoiler
I haven't heard of /t so I may be inaccurate.He literally just told you.
Take your string and add the /t escape to it.
print(Helloe\tWorld!)
Edited on 16 April 2014 - 10:19 PM
Posted 17 April 2014 - 12:30 AM
Go to: http://coronalabs.co...a-string-magic/
Copy the string:split function and use it with your string like this:
This should work, though I did not test this snippet!
For more info just google "lua string split". You'll get a ton of hits that can be helpful to you.
Copy the string:split function and use it with your string like this:
cells = string.split( stringtosplit, "\t" )
cells is the variable where the splitted substring will be in, stringtosplit is the string you want to split at the tabs and "\t" is the so called delimiter.This should work, though I did not test this snippet!
For more info just google "lua string split". You'll get a ton of hits that can be helpful to you.
Posted 17 April 2014 - 12:57 AM
this would or would not create a table with MULTIPLE split parts. so "1/t2/t/3" would become {1, 2, 3}. Right?Go to: http://coronalabs.co...a-string-magic/
Copy the string:split function and use it with your string like this:cells is the variable where the splitted substring will be in, stringtosplit is the string you want to split at the tabs and "\t" is the so called delimiter.cells = string.split( stringtosplit, "\t" )
This should work, though I did not test this snippet!
For more info just google "lua string split". You'll get a ton of hits that can be helpful to you.
Posted 17 April 2014 - 01:10 AM
invoking the split like sothis would or would not create a table with MULTIPLE split parts. so "1/t2/t/3" would become {1, 2, 3}. Right?
string.split( "1/t2/t/3", "/t" )
it would actually return
{"1", "2", "/3"}
Edited on 16 April 2014 - 11:10 PM
Posted 17 April 2014 - 11:00 AM
right. Alright, thanks! So I DON'T need to encase every answer with quotes?
Posted 17 April 2014 - 02:21 PM
okay, so, this code would work?
row[1] = "1 /t 2 /t 3"
newrow[1] = string.split(row[1], "/t")
newrow[1] == {"1 ", " 2 ", " 3"}
right?Posted 17 April 2014 - 03:35 PM
No, since you didn't define row or newrow as tables >.> Can't you just test it though? Jeez…
Posted 17 April 2014 - 04:24 PM
kk ty
Posted 17 April 2014 - 04:57 PM
Keep in mind that /t is not the same thing as \t.