Split a string to fields
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Split a string to fields
lets say I have
Put "1 2 3" into String
how would I split String into fld Number1, fld Number2, fld Number3?
Put "1 2 3" into String
how would I split String into fld Number1, fld Number2, fld Number3?
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här
Re: Split a string to fields
Hi.
How much experience do you have with writing handlers?
Basically, since you seem to have three words in your string, and want to put each word into its own field, it depends on how you reference those fields. In your example, you indicate that the fields are named "number1", "number2". etc. Is this really so, or did you just mean field 1, field 2, etc.?
Anyway, you would set up a repeat loop: (pseudocode)
Now "fieldReference" can be by number or any other method. For example, if you really meant "field2", you have a constant word "field" concatenated to a number, "2". Can you get your head around the following reference:
put word y of yourString into field "field" & y --puts the "2", say, into field "field & 2" or field "field2
Write back if you need more help with this.
Craig Newman
How much experience do you have with writing handlers?
Basically, since you seem to have three words in your string, and want to put each word into its own field, it depends on how you reference those fields. In your example, you indicate that the fields are named "number1", "number2". etc. Is this really so, or did you just mean field 1, field 2, etc.?
Anyway, you would set up a repeat loop: (pseudocode)
Code: Select all
repeat with y = 1 to the number of words of yourstring
put word y of yourString into field fieldReference
end repeat
put word y of yourString into field "field" & y --puts the "2", say, into field "field & 2" or field "field2
Write back if you need more help with this.
Craig Newman
Re: Split a string to fields
You asked about experience, I am very new to livecode, I have been playing with it for 2 weeks now..
I was trying to make my question simple but I think I made it harder.
I have been working with the livecode - Connecting to a MySQL database tutorial. and I have a database with 19 tables.
Here is my code, its probably sloppy, try not to make fun of me
at the very end there:
I get the data from one line in my data in a long string separated by space. ( I can change this to be tab or comma )
so right now as an example I get "1 2 3 4" in my tData variable
what i need instead is "1" in field "job", "2" in field "certn", "3" in field.... so on and so on
maybe there is a better way for me to query the database to fill in the fields.
This question at this point probably needs to be moved to the database forums
I was trying to make my question simple but I think I made it harder.
I have been working with the livecode - Connecting to a MySQL database tutorial. and I have a database with 19 tables.
Here is my code, its probably sloppy, try not to make fun of me

Code: Select all
on closeField
-- check the global connection ID to make sure we have a database connection
global gConnectionID
if gConnectionID is not a number then
answer error "Please connect to the database first."
exit to top
end if
-- edit these variables to match your database & table
-- this assumes a table called Table1 with 3 fields
put "tracker" into tTableName
put "trackerjob, trackercert" into tFields
put fld "jobn" into tJobN
put fld "certn" into tCertN
-- construct the SQL - the :1, :2 & :3 placeholders in the SQL will be filled by variables in the revExecuteSQL line
put "INSERT INTO " & tTableName & " (" & tFields & ") VALUES (:1, :2)" into tSQL
-- send the SQL to the database, filling in the placeholders with data from variables
revExecuteSQL gConnectionID, tSQL, "tJobN", "tCertN"
-- check the result and display the data or an error message
if the result is a number then
answer info "New record added."
else
put "SELECT * FROM " & tTableName & " WHERE trackercert=" & tCertN into tSQL
-- query the database
put revDataFromQuery(Tab, cr, gConnectionID, tSQL) into tData
-- check the result and display the data or an error message
if item 1 of tData = "revdberr" then
answer error "There was a problem querying the database:" & cr & tData
else
put item 1 of tData into field "view"
end if
end if
end closeField
Code: Select all
else
put item 1 of tData into field "view"
end if
end if
end closeField
so right now as an example I get "1 2 3 4" in my tData variable
what i need instead is "1" in field "job", "2" in field "certn", "3" in field.... so on and so on
maybe there is a better way for me to query the database to fill in the fields.
This question at this point probably needs to be moved to the database forums
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här
Re: Split a string to fields
Hi DavJans,
to build a string with all your field names in the correct order (like the data from db) and do a repeat loop.
Something like in this example:
You get the picture
Best
Klaus
OK, the data you have in myData is TAB delimited, so ONE way to make your life easier could beso right now as an example I get "1 2 3 4" in my tData variable
what i need instead is "1" in field "job", "2" in field "certn", "3" in field.... so on and so on
to build a string with all your field names in the correct order (like the data from db) and do a repeat loop.
Something like in this example:
Code: Select all
...
set itemdel to TAB ## !!!
put "job,certn,name_of_field_3" into tFieldNames
## MUCH easier than to type -> put "sdsdsd" & TQB & "sdsdsd" & TAB & "xcxcxc" & TAB & "xcxcxc"... :-D
replace "," with TAB in tFieldNames
## Now loop through the record from database:
repeat with i = 1 to the num of items of myData
put item i of myData into fld (item i of tFieldNames)
end repeat
...

Best
Klaus
Re: Split a string to fields
Thank you, I just figured out a way to do this and came back to post it, when I found your reply.
here is what I did to fix it
changed:
what this did for me is instead of item 1 here:
equal to "1 2 3 4"
Item 1 is now "1"
item 2 is "2"
item 3 is "3" and so on
then I changed:
here is what I did to fix it
changed:
Code: Select all
put revDataFromQuery(Tab, cr, gConnectionID, tSQL) into tData
to
put revDataFromQuery(Comma, cr, gConnectionID, tSQL) into tData
Code: Select all
put item 1 of tData into field "view"
Item 1 is now "1"
item 2 is "2"
item 3 is "3" and so on
then I changed:
Code: Select all
put item 1 of tData into field "jobn"
put item 1 of tData into field "certn"
put item 1 of tData into field "field3"
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här
Re: Split a string to fields
Sorry, no capisce!? 

Re: Split a string to fields
Your way of doing it would have been a lot less typing
and I may change it. I got one question for you tho, say someone puts a comma into a field as part of the data I assume that will mess everything up

"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här
Re: Split a string to fields
Hi DavJans,
Best
Klaus
with COMMA as itemdelimiter: yes!DavJans wrote:Your way of doing it would have been a lot less typingand I may change it. I got one question for you tho, say someone puts a comma into a field as part of the data I assume that will mess everything up
Best
Klaus