What I want to do: Paste 3 columns of tab delimited data into a text area, click an import button, and write the columns into a XOJO database.
Something like:
123456789
321654987
I built a text area (called var1), and was able to read the first row, first column into the DB using:
Dim var1 As String = var1.Text
var1 = var1.Mid(1,7)
DB and import code are done, so I clicked import, and it worked fine. Then I realized that my text lengths are going to be different, and I need to somehow jump a tab to get to column 2.
Perhaps left, mid and right are not the right commands to use. Can anyone suggest a code snippet or other commands for me to read up on…
Thanks Anthony. Chr 9 is TAB, I am assuming, and you are splitting my large string into smaller ones using Chr 9 as the delimiter. Do I end up with lots of smaller strings? Is strInput replaced with the name of the text area?
Trying to hang with this… sorry for the Kgarden questions.
That will create an array of strings with each substring of the tab-delimited original. If you want to use the text from a textfield or textarea, you’d just just replace strInput with var1.Text.
Check the documentation for arrays to see how to use the result.
You can also do this in two stages which may make it easier.
First split the input into individual rows:
dim Rows() as string = split(strInput, endofline)
Then handle each row:
for i as integer = 0 to ubound(Rows)
dim Columns() as string = split(Rows(i), chr(9))
// Now each element of Columns is a single item of the current row and column
next
Thanks guys… for Roberts example I think I need to put the import statement in place of his commented line. Not sure how I control col and row now… but I’ll keep messing with it.
' Read the text from your textarea, and put each
' row into a separate element of the Rows array
dim Rows() as string = split(myTextArea.text, endofline)
for i as integer = 0 to ubound(Rows)
' Now split the tab delimited text of the current row into
' separate elements in the Columns array
dim Columns() as string = split(Rows(i), chr(9))
'Another loop to go through each item in the current row:
For j as integer = 0 to ubound(Columns)
dim currentTextItem as string = Columns(j)
' At this point in the inner loop you can process the
' individual text items (put them into your database, etc.)
next
next
Thanks. The code I was using for the import of a row is from the basic baseball team tutorial from the XOJO folks.
If AddMatchRow(var1 , var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14, var15, var16, var17) Then
AddDataStatusLabel.Text = “Data added.”
Else
AddDataStatusLabel.Text = “Error.”
End If
If I’m following the code above from my patient friends, it looks like I have to somehow break this down - one by one- and process inside the inner loop. Is that what Robert was referring to?
My first guess was to replace all my var variables with currentTextItem. It worked, but it treated each variable as a row, and wrote the variable into the db 17 times, then did row 2 17 times, etc.
Almost seems like this is like Spock and his 3d chess board. I have to know how to push cols and rows at the same time.
I’ve gotten it to read the first row of my pasted data, and place the 17 variables in the 17 columns of the table… using this code. Yeah! Anyone see a reason why it won’t read the second row that I pasted?
’ Read the text from your textarea, and put each
’ row into a separate element of the Rows array
dim Rows() as string = split(myTextArea.text, endofline)
for i as integer = 0 to ubound(Rows)
’ Now split the tab delimited text of the current row into
’ separate elements in the Columns array
dim Columns() as string = split(Rows(i), chr(9))
'Another loop to go through each item in the current row:
'For j as integer = 0 to ubound(Columns)
'dim currentTextItem as string = Columns(j)
’ At this point in the inner loop you can process the
’ individual text items (put them into your database, etc.)
If AddMatchRow(Columns(0), Columns(1), Columns(2), Columns(3), Columns(4), Columns(5), Columns(6), Columns(7), Columns(8), Columns(9), Columns(10), Columns(11), Columns(12), Columns(13), Columns(14), Columns(15), Columns(16)) Then
You can also simplify. The way you’re doing it, you don’t need the nested loop from what I can see:
[code]’ Read the text from your textarea, and put each
’ row into a separate element of the Rows array
dim Rows() as string = split(ReplaceLineEndings(myTextArea.text, EndOfLine), endofline)
for i as integer = 0 to ubound(Rows)
’ Now split the tab delimited text of the current row into
’ separate elements in the Columns array
dim Columns() as string = split(Rows(i), chr(9))
If AddMatchRow(Columns(0), Columns(1), Columns(2), Columns(3), Columns(4), Columns(5), Columns(6), Columns(7), Columns(8), Columns(9), Columns(10), Columns(11), Columns(12), Columns(13), Columns(14), Columns(15), Columns(16)) Then
AddDataStatusLabel.Text = “Data added.”
Else
AddDataStatusLabel.Text = “Error.”
End If
The trick was ReplaceLineEndings. Since you’re going through a TextArea, you need to be aware that TextArea does not use the normal EndOfLine marker. The text you paste in is not actually the same as the text you pull out of the TextArea. The line endings have changed. ReplaceLineEndings changes them back.
Thanks. The text i’m pasting… 17 cols of tabbed data (formerly in a table) … has about 15 rows of header data that I don’t need to process.
Whats the easiest way to get rid of this stuff? regex? or if I know it’s all always the first 15 rows, can I do something like “delete rows 1-15”, and start processing the script starting with row 16?