combine two fields

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

mister
Posts: 39
Joined: Thu Nov 27, 2014 9:03 pm

combine two fields

Post by mister » Sat Jan 18, 2020 10:59 pm

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

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: combine two fields

Post by kaveh1000 » Sun Jan 19, 2020 1:20 pm

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?
Kaveh

mister
Posts: 39
Joined: Thu Nov 27, 2014 9:03 pm

Re: combine two fields

Post by mister » Sun Jan 19, 2020 6:00 pm

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
array test.zip
(1.47 KiB) Downloaded 260 times
Thanks, Larry

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: combine two fields

Post by kaveh1000 » Sun Jan 19, 2020 6:09 pm

OK. This seems to work as a script:

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 mouseup
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. :-)
Kaveh

mister
Posts: 39
Joined: Thu Nov 27, 2014 9:03 pm

Re: combine two fields

Post by mister » Sun Jan 19, 2020 6:25 pm

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. :shock:
Larry

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: combine two fields

Post by kaveh1000 » Sun Jan 19, 2020 6:29 pm

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
Kaveh

mister
Posts: 39
Joined: Thu Nov 27, 2014 9:03 pm

Re: combine two fields

Post by mister » Sun Jan 19, 2020 7:28 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: combine two fields

Post by Klaus » Sun Jan 19, 2020 7:43 pm

Hi Larry,

in your first example you wrote:

Code: Select all

...
set the itemDel to column
...
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... :D
Try it out, seeing is believing :D

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 mouseUp
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

mister
Posts: 39
Joined: Thu Nov 27, 2014 9:03 pm

Re: combine two fields

Post by mister » Mon Jan 20, 2020 5:42 pm

Hi Klaus,

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" 
and the answer was "false" until
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"
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.

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 mouseUp
Thanks,

Larry

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: combine two fields

Post by Klaus » Mon Jan 20, 2020 5:56 pm

Hi Larry,

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 mouseUp
Do like this:

Code: 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 mouseUp
Best

Klaus

mister
Posts: 39
Joined: Thu Nov 27, 2014 9:03 pm

Re: combine two fields

Post by mister » Mon Jan 20, 2020 7:10 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: combine two fields

Post by Klaus » Mon Jan 20, 2020 7:21 pm

Hi Larry,

waht do you mean by:
but the cr is not working.
?
Please post your script!


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: combine two fields

Post by dunbarx » Mon Jan 20, 2020 7:25 pm

Just an aside, the itemDelimiter can be any string. So it is indeed possible to:

Code: Select all

 set the itemDel to  tab & return
It may or may not be useful in this discussion, but does work just fine.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: combine two fields

Post by dunbarx » Mon Jan 20, 2020 7:28 pm

Hi.

A quick test here of Klaus' offering worked fine.

When he said:
## Presumed you have a TAB as itemdel!
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.

Craig

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: combine two fields

Post by Klaus » Mon Jan 20, 2020 7:41 pm

dunbarx wrote:
Mon Jan 20, 2020 7:25 pm
Just an aside, the itemDelimiter can be any string. So it is indeed possible to:

Code: Select all

 set the itemDel to  tab & return
It may or may not be useful in this discussion, but does work just fine.
Yes, but not:

Code: Select all

...
set the itemDel to tab AND return
...

Post Reply