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

Take apart sections of strings.

Started by MrabEzreb, 16 April 2014 - 07:17 PM
MrabEzreb #1
Posted 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!
DerKoch #2
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
MrabEzreb #3
Posted 16 April 2014 - 09:40 PM
? escape sequence? i have no idea what u r talking about. :P/>
DerKoch #4
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

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.
MrabEzreb #5
Posted 16 April 2014 - 11:49 PM
so… how would I use that?
Agoldfish #6
Posted 17 April 2014 - 12:17 AM
Disclaimer:
SpoilerI 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
DerKoch #7
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:

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.
MrabEzreb #8
Posted 17 April 2014 - 12:57 AM
Go to: http://coronalabs.co...a-string-magic/
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.
this would or would not create a table with MULTIPLE split parts. so "1/t2/t/3" would become {1, 2, 3}. Right?
theoriginalbit #9
Posted 17 April 2014 - 01:10 AM
this would or would not create a table with MULTIPLE split parts. so "1/t2/t/3" would become {1, 2, 3}. Right?
invoking the split like so

string.split( "1/t2/t/3", "/t" )
it would actually return

{"1", "2", "/3"}
Edited on 16 April 2014 - 11:10 PM
MrabEzreb #10
Posted 17 April 2014 - 11:00 AM
right. Alright, thanks! So I DON'T need to encase every answer with quotes?
MrabEzreb #11
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?
CometWolf #12
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…
MrabEzreb #13
Posted 17 April 2014 - 04:24 PM
kk ty
Bomb Bloke #14
Posted 17 April 2014 - 04:57 PM
Keep in mind that /t is not the same thing as \t.