comparing 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

Post Reply
Andycal
Posts: 144
Joined: Mon Apr 10, 2006 3:04 pm

comparing two fields

Post by Andycal » Thu Feb 07, 2013 2:20 pm

I've got two fields each one has a list of product codes in them, but the second one has fewer codes.

What I'm trying to do is check to see if each product from the first field is in the second field. If it's not, pop the code into a third field.

Sounds easy, but I'm baffled. The find command just puts a box around the text, how do I actually do something with it?

Andycal
Posts: 144
Joined: Mon Apr 10, 2006 3:04 pm

Re: comparing two fields

Post by Andycal » Thu Feb 07, 2013 2:36 pm

OK, I found the 'Not found' bit in the docs, however the find starts from the last found, so I've got this:

Code: Select all

on mouseUp
   repeat for each line tLine in field one
      find string item one of tLine in field two
      if the result is "Not found" then put tLine & CR after field three
   end repeat
end mouseUp
However it will miss any products higher up in the field. Can you reset to the beginning?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: comparing two fields

Post by dunbarx » Thu Feb 07, 2013 2:46 pm

Hi.

Try this. I hope you see which fields are which.

Code: Select all

on mouseUp
   put fld "masterList" into masterList
   put fld "fieldToCheck" into fieldToCheck
   repeat for each line tLine in masterList
      if tLine is not among the lines of fieldToCheck then put tLine & return after fld "missingLines"
   end repeat
end mouseUp
Please look up the repeat and "is among" stuff in the dictionary.

Craig Newman

Klaus
Posts: 13822
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: comparing two fields

Post by Klaus » Thu Feb 07, 2013 2:53 pm

Hi Andy,

don't use FIND here, instead use LINEOFFSET!

fld 1 = the one with MORE content
fld 2 = the one with LESS content
fld 3 = for the differences, right?

OK, do this:

Code: Select all

...
## Do not access fields directly, too much overhead behind the scenes (read SLOW!), 
## put their contents into variables and work with these!
put fld 1 into tMoreContent
put fld 2 into tLessContent

## For a 100% 1:1 comparison: "This string" <> "this string"
## check "wholematches" in the dictionary
set the wholematches to true
repeat for each line tLine in tMoreContent
  if lineoffset(tLine,tLessContent) = 0 then
     ## line NOT in fld 2!
    put tLine & CR after tDiff
  end if
end repeat

## Get rid of trailing CR
delete char -1 of tDiff

## NOW put tDiff into field 3 "en bloc" :-)
put tDiff into fld 3
...
Best

Klaus

Andycal
Posts: 144
Joined: Mon Apr 10, 2006 3:04 pm

Re: comparing two fields

Post by Andycal » Thu Feb 07, 2013 2:58 pm

dunbarx wrote:Hi.

Try this. I hope you see which fields are which.

Code: Select all

on mouseUp
   put fld "masterList" into masterList
   put fld "fieldToCheck" into fieldToCheck
   repeat for each line tLine in masterList
      if tLine is not among the lines of fieldToCheck then put tLine & return after fld "missingLines"
   end repeat
end mouseUp
Please look up the repeat and "is among" stuff in the dictionary.

Craig Newman
Worked a treat! Thanks!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”