Checking a field list

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Preston Shea
Posts: 73
Joined: Sat Apr 08, 2006 6:51 pm

Checking a field list

Post by Preston Shea » Mon Feb 28, 2011 9:10 pm

Some of the lines in field A are in field B as well. I want to go down the lines in field A and, if line i of fld A is in fld B, put ln i of fld A into fld C. If ln i of fld A is not in fld B, I want to put ln i of fld A into fld D.

I have been trying to do this with nested loops but can't get it to work right. Any help would be very welcome!
In nova fert animus mutatas dicere formas corpora.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Checking a field list

Post by BvG » Mon Feb 28, 2011 9:43 pm

why would you nest loops to do that?

Code: Select all

repeat for each line theLine in fieldA
  if theLine is among the lines of fieldB then
    put theLine & return after fieldC
  else
    put theLine & return after fieldD
  end if
end repeat
delete char -1 of fieldC
delete char -1 of fieldD
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

Preston Shea
Posts: 73
Joined: Sat Apr 08, 2006 6:51 pm

Re: Checking a field list

Post by Preston Shea » Mon Feb 28, 2011 10:50 pm

Thank you so much!
In nova fert animus mutatas dicere formas corpora.

Preston Shea
Posts: 73
Joined: Sat Apr 08, 2006 6:51 pm

Re: Checking a field list

Post by Preston Shea » Tue Mar 01, 2011 1:11 pm

How would you modify this code to sort by the first word of each word of each line in fieldA instead of the entire line?

repeat for each line theLine in fieldA
if theLine is among the lines of fieldB then
put theLine & return after fieldC
else
put theLine & return after fieldD
end if
end repeat
delete char -1 of fieldC
delete char -1 of fieldD
In nova fert animus mutatas dicere formas corpora.

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

Re: Checking a field list

Post by dunbarx » Tue Mar 01, 2011 8:22 pm

Hi.

Do you mean that if the first word of a line in fld A is anywhere is fld B? In that case:

repeat for each line theLine in fieldA
if word 1 of theLine is in fieldB then
etc.

Craig Newman

Preston Shea
Posts: 73
Joined: Sat Apr 08, 2006 6:51 pm

Re: Checking a field list

Post by Preston Shea » Tue Mar 01, 2011 10:22 pm

Sorry, I didn't explain clearly. For each line thisLine in fieldA, If the first word in thisLine of fieldA = the first word in any line of fieldB, then ...

also, is there a way to deal with conditions in which the first word in thisLine of fieldA = the first word in two or more different lines in fieldB?
In nova fert animus mutatas dicere formas corpora.

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

Re: Checking a field list

Post by dunbarx » Wed Mar 02, 2011 1:09 am

OK.

So now you need to map the lines of one field to the lines in the other. A different form of repeat is best here:

Code: Select all

on mouseup
   repeat with y = 1 to the number of lines of fld "fieldA"
      if word 1 of line y of fld "fieldA" = word 1 of line y of fld "fieldB" then
         put line y of fld "fieldA" into line y of fld "fieldC"
      else
         put line y of fld "fieldA" into line y of fld "fieldD"
      end if
   end repeat
end mouseup


You may need a bit of houseKeeping, if the number of lines of fld A is not the same as field B, and maybe other stuff. But you should get the idea.

In the second case:

Code: Select all

on mouseup
   put "" into fld "fieldC"
   put "" into fld "fieldD"
   repeat with y = 1 to the number of lines of fld "fieldA"
      repeat with u = 1 to the number of lines of fld "fieldB"
         if word 1 of line y of fld "fieldA" = word 1 of line u of fld "fieldB" then
            put u & ":" && line y of fld "fieldA" & return after fld "fieldC"
         end if
      end repeat
      end repeat
end mouseup
Not sure exactly how you want the output, so I just put the line number(s) in fleldB that matched word 1 of each line in fieldA. Try some data and see if this works. If not, ask again.

Craig Newman

Post Reply