combine two fields
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
combine two fields
I'm going to try and give it another shot. I've not used LiveCode since 2017. Back to beginner status. Already stumped.
In a test field, I'm trying to merge two fields into one using an array with split and combine.
Field a1 contains - dog > mouse > toad Field a2 contains - cat > no.1 > 10
>=tab
global bSave, fSave
on mouseUp
set the itemDel to column
put fld "a1" into fSave
split fSave by column
answer fSave[2]
put fSave into fld "b1"
put fld "a2" into fSave[4]
--answer fSave[4]
combine fSave by column
put fSave into fld "b1"
end mouseUp
It works in a weird way, but I'd like to delete "cat' column and combine the fields. I've tried , return and tabs as delimiters, numerous repeat loops before and after the combination of the fields and haven't found any that work. Or do I need to put field b1 into another array and sort? Does this make any sense?
Thanks, Larry
			
			
									
									
						In a test field, I'm trying to merge two fields into one using an array with split and combine.
Field a1 contains - dog > mouse > toad Field a2 contains - cat > no.1 > 10
>=tab
global bSave, fSave
on mouseUp
set the itemDel to column
put fld "a1" into fSave
split fSave by column
answer fSave[2]
put fSave into fld "b1"
put fld "a2" into fSave[4]
--answer fSave[4]
combine fSave by column
put fSave into fld "b1"
end mouseUp
It works in a weird way, but I'd like to delete "cat' column and combine the fields. I've tried , return and tabs as delimiters, numerous repeat loops before and after the combination of the fields and haven't found any that work. Or do I need to put field b1 into another array and sort? Does this make any sense?
Thanks, Larry
Re: combine two fields
Hi Larry
Just thinking you might be overcomplicating things. Not sure why you need arrays. Can you be more specific about what the text actually looks like in the fields and how you want to them to look in the final field. Does fld 1 just have each word on a separate line and same for field 2?
And do you just want to add the contents of field 2 to the end of field 1?
			
			
									
									Just thinking you might be overcomplicating things. Not sure why you need arrays. Can you be more specific about what the text actually looks like in the fields and how you want to them to look in the final field. Does fld 1 just have each word on a separate line and same for field 2?
And do you just want to add the contents of field 2 to the end of field 1?
Kaveh
						Re: combine two fields
Hi Kaveh,
I always over complicate things
 and yes, i want the contents of field 2 after the contents of field 1.  I tried many repeat loops (not familiar enough with "nested or next") before jumping over to the "How to Lessons on Arrays".  If  a repeat loop or anything else works, I'm all ears.
Here's my test stack
Thanks, Larry
			
			
									
									
						I always over complicate things
Here's my test stack
Thanks, Larry
Re: combine two fields
OK. This seems to work as a script:
I tend to break lines to make them more readable. 
You can make it more foolproof, e.g. by checking number of lines are the same, no missing lines, etc.
I continue to struggle with arrays. Thankfully you don't need it in this case.
			
			
									
									Code: Select all
on mouseup
   put empty into fld "b1"
   repeat with L = 1 to the number of lines of fld "a1"
      put Line L of fld "a1" &  \
            tab &  \
            line L of fld "a2"  \
            into line L of fld "b1"
   end repeat
end mouseupYou can make it more foolproof, e.g. by checking number of lines are the same, no missing lines, etc.
I continue to struggle with arrays. Thankfully you don't need it in this case.
Kaveh
						Re: combine two fields
Kaveh,
Thank you!
i never tried using "whole lines" before, always item 1 of line x .....etc. So simple, yet i went around the world to try and figure it out.
  
Larry
			
			
									
									
						Thank you!
i never tried using "whole lines" before, always item 1 of line x .....etc. So simple, yet i went around the world to try and figure it out.
Larry
Re: combine two fields
And I am proud because I am usually the recipient of help from the planet-heads on this forum. Thanks for giving me the opportunity to give back in a small way. 
I am doing a lot with text processing so happy to exchange info on that.
You tend forget how elegant and simple LiveCode is, compared with professional dev tools! But even more powerful than those.
Keep with it Larry.
Kaveh
			
			
									
									I am doing a lot with text processing so happy to exchange info on that.
You tend forget how elegant and simple LiveCode is, compared with professional dev tools! But even more powerful than those.
Keep with it Larry.
Kaveh
Kaveh
						Re: combine two fields
Kaveh,
'i guess i got excited that a simple repeat loop worked that i over looked the fact that i only wanted field a1 and item 2 and item 3 of field "a2" into field b1. Sorry to jump the gun. i'll keep working on it.
Keep helping where and when you can!
Larry
			
			
									
									
						'i guess i got excited that a simple repeat loop worked that i over looked the fact that i only wanted field a1 and item 2 and item 3 of field "a2" into field b1. Sorry to jump the gun. i'll keep working on it.
Keep helping where and when you can!
Larry
Re: combine two fields
Hi Larry,
in your first example you wrote:
but that is no constant in LC but a reserved word, it actually means:
set the itemdelimiter to (the string "column")
Which will work with text like:
This is item one of the text...columnThis is item two of the text...columnAnd item three etc...
 
Try it out, seeing is believing
 
You get the picture...
Whatever, you want to do something like this:
line 1 of fld "a1" and item 2 of line 1 of fld "a2" and item 3 of line 1 of fld "a2" -> line 1 of fld "b1"
etc. for all other lines...
Is that what you are trying to do?
Best
Klaus
			
			
									
									
						in your first example you wrote:
Code: Select all
...
set the itemDel to column
...set the itemdelimiter to (the string "column")
Which will work with text like:
This is item one of the text...columnThis is item two of the text...columnAnd item three etc...
Try it out, seeing is believing
Code: Select all
on mouseUp 
   put "This is item one of the text...columnThis is item two of the text...columnAnd item three etc..." into tText
   set itemdel to column
   put item 2 of tText
   ## -> This is item two of the text...
end mouseUpWhatever, you want to do something like this:
line 1 of fld "a1" and item 2 of line 1 of fld "a2" and item 3 of line 1 of fld "a2" -> line 1 of fld "b1"
etc. for all other lines...
Is that what you are trying to do?
Best
Klaus
Re: combine two fields
Hi Klaus,
 
Klaus said,
Yes, disregarding my original code, i put this into the button.  and the answer was "false" until
i changed it to
the result was fld "a1" only. And now either snippet returns the same result ..  only the text of fld "a1'  in fld  "b1". no "a2"
This is getting overly complicated. i think i have "fried" this stack, is that possible? I've opened in vers. 8.0 and 9.5, bombarded it with lines of code switched variables, etc.
What i truly need is a repeat loop for the entire field.
This code below is probably wrong, but i have tried several different versions repeat with... repeat for each... to no avail.
Thanks, 
Larry
			
			
									
									
						Klaus said,
Whatever, you want to do something like this:
line 1 of fld "a1" and item 2 of line 1 of fld "a2" and item 3 of line 1 of fld "a2" -> line 1 of fld "b1"
etc. for all other lines...
Is that what you are trying to do?
Yes, disregarding my original code, i put this into the button.
Code: Select all
put line 1 of fld "a1" & item 2 of line 1 of fld "a2" & item 3 of line 1 of fld "a2" into line 1 of fld "b1" i changed it to
Code: Select all
put line 1 of fld "a1" & tab & item 2 of line 1 of fld "a2" & tab & item 3 of line 1 of fld "a2" into line 1 of fld "b1"This is getting overly complicated. i think i have "fried" this stack, is that possible? I've opened in vers. 8.0 and 9.5, bombarded it with lines of code switched variables, etc.
What i truly need is a repeat loop for the entire field.
This code below is probably wrong, but i have tried several different versions repeat with... repeat for each... to no avail.
Code: Select all
on mouseUp
   put empty into fld "b1" 
   set the itemDel to return and tab
   put fld "a1" into tData
   repeat with x = 1 to the number of lines  in tData   -- for each line tLine
      put item 1 of  tData and item 2 of fld "a2"  into x    --tLine --fld "b1"
      put  x into fld "b1" -- tLIne
   end repeat 
end mouseUpLarry
Re: combine two fields
Hi Larry,
see my comments
Do like this:
Best
Klaus
			
			
									
									
						see my comments
Code: Select all
on mouseUp
   put empty into fld "b1" 
   
   ## We can only have ONE itemdelimiter, so this will fail!
   set the itemDel to return and tab
   put fld "a1" into tData
   repeat with x = 1 to the number of lines  in tData   -- for each line tLine
      put item 1 of  tData and item 2 of fld "a2"  into x    --tLine --fld "b1"
    
      ## You overwrite fld "b1" in every loop!
      put  x into fld "b1" -- tLIne
   end repeat 
end mouseUpCode: Select all
on mouseUp
   ## Not really neccessary, since we overwrite this field with new data!
   ## put empty into fld "b1" 
   
   ## Presumed  you have a TAB as itemdel!
   set itemDel to tab
   
   ## Accessing fields is 1000000 x slower tht accessing variables!
   put fld "a1" into tData
   put fld "a2" into tData2
   
   ## We fist collect all data and then puot into field "en bloc"
   put empty into tData3
   
   repeat with x = 1 to the number of lines in tData
      put line x of tData & tab & item 2 of line x of tData2 & tab & item 3 of line x of tData2 & CR after tData3
   end repeat 
   
   ## Remove trailing CR
   delete char -1 of tData3
   
   ## Now fill field:
   put tData3 into fld "b1"
end mouseUpKlaus
Re: combine two fields
Klaus,
Sorry to bother you and I have tried some fixes, but the cr is not working. The text is all on the first line.
Thanks,
Larry
			
			
									
									
						Sorry to bother you and I have tried some fixes, but the cr is not working. The text is all on the first line.
Thanks,
Larry
Re: combine two fields
Hi Larry,
waht do you mean by:
Please post your script!
Best
Klaus
			
			
									
									
						waht do you mean by:
?but the cr is not working.
Please post your script!
Best
Klaus
Re: combine two fields
Just an aside, the itemDelimiter can be any string. So it is indeed possible to:
It may or may not be useful in this discussion, but does work just fine.
Craig
			
			
									
									
						Code: Select all
 set the itemDel to  tab & returnCraig
Re: combine two fields
Hi.
A quick test here of Klaus' offering worked fine.
When he said:
Craig
			
			
									
									
						A quick test here of Klaus' offering worked fine.
When he said:
he meant that to be so in the source fields, that the data was already tab delimited, not that the itemDel was set and used in the handler. You have to be careful , in that tabs are invisible, and are often not obvious.## Presumed you have a TAB as itemdel!
Craig
Re: combine two fields
Yes, but not:dunbarx wrote: ↑Mon Jan 20, 2020 7:25 pmJust an aside, the itemDelimiter can be any string. So it is indeed possible to:It may or may not be useful in this discussion, but does work just fine.Code: Select all
set the itemDel to tab & return
Code: Select all
...
set the itemDel to tab AND return
...
