LiveCode LessonsHow To - LiveCode Sample Scripts Graphics and ObjectsFinding Uncommon Lines in Two Containers (Copy)

Finding Uncommon Lines in Two Containers (Copy)

This lesson demonstrates how to find the uncommon lines in two containers, this is a useful function for comparing lists.

The UI

The UI

The UI for this example consists of two fields, each containing some text, and a button to find the lines that are common between the two fields,

You can download the stack associated with this lesson from this url https://tinyurl.com/y6wwmwyq

The uncommonLines function

The uncommonLines function takes two lists, finds the lines that are in one list, but not in the other, and marks them with a leading "+" or "-" depending on which of the two lists the line is found in. If you start with the first list, add the lines marked with "+", and delete the lines marked with "-", you get the second list.

put uncommonLines(field "Original",field "New") into tLines

Finding lines in the first list that are not in the second list

The handler goes through each list in turn, and builds a third list consisting of the lines that the lists don't have in common. First, it uses a repeat control structure to scan each line in the pFirstList parameter. If the line doesn't also appear in the pSecondList parameter, the handler adds that line to a variable called tUncommon, prepending a "-" to the line with the && operator. (The && operator places a space between the "-" and the text of the line for better readability.) It also adds a return character so that the next line added will start on a new line.

repeat for each line tLine in pFirstList
	if tLine is not in pSecondList then 
		## include this line
		put "-" && tLine & return after tUncommon
	end if
end repeat

Next, the handler uses a similar repeat loop to scan each line in pSecondList. If the line isn't in the pFirstList, the handler adds the line to the tUncommon variable, prepending a "+" this time.

Returning the uncommon lines

Since the handler adds a return character to tUncommon after each uncommon line it finds, the last character of tUncommon is a trailing return. The handler deletes this trailing return and then returns the contents of the tUncommon variable.

if last char of tUncommon is return then
	## strip trailing return
	delete last char of tUncommon
end if
return tUncommon

The uncommonLines function code

This function is added to the card script

function uncommonLines pFirstList, pSecondList
	local tUncommon
   
	## find lines in the first list but not the second list
	repeat for each line tLine in pFirstList
		if tLine is not in pSecondList then 
			## include this line
			put "-" && tLine & return after tUncommon
		end if
	end repeat
   
	## find lines in the second list but not the first list
	repeat for each line tLine in pSecondList
		if tLine is not in pFirstList then 
			## include this line
			put "+" && tLine & return after tUncommon
		end if
	end repeat
   
	if last char of tUncommon is return then
		## strip trailing return
		delete last char of tUncommon
	end if
	return tUncommon
end uncommonLines

A note on efficiency

To make this handler faster, we've used the repeat for each form of the repeat control structure. A more traditional way to write the first repeat loop would be like this:

repeat with x = 1 to the number of lines in pFirstList
	if line x of pFirstList is not in pSecondList then
		put "-" && line x of pFisrtList & return after tUncommon
	end if
end repeat

Many languages support this form of repeat loop, and Transcript does too: this form will work the same way as the one we used. However, the repeat for each form is much faster--depending on the circumstances, up to hundreds of times faster--than the repeat with x = start to end form.

0 Comments

Add your comment

E-Mail me when someone replies to this comment