Data validation, thoughts please...

Hi there,

I’ve written a script for generating text from information supplied in Excel.
The script works fine however I’m just in the process of adding some error handling.

I have a subroutine that generates the text from the parameters passed in. It’s these parameters I’m wanting to check.

I call the subroutine like this:-

generateText(“W,A,D,E”, “<@interReg>,<@interReg>,<@interBold>,<@interReg>”, “T,N,T,T”>

As you can see there are three parameters in this example each containing 4 parts, each part separated by a comma:-
“W,A,D,E”
“<@interReg>,<@interReg>,<@interBold>,<@interReg>”
“T,N,T,T”

What I’d like to is check that each parameter contains the same number of parts up to a maximum of 8.

I originally wrote some code that takes the number of text items in each and multiplies them together so the answer should be 4x4x4 = 64 in the example above. If the answer is something different then this would indicate that’s there’s a problem. Of course there are some anomalies with this approach.

One main problem with this method is that I was using text item delims to check the number of parts in each.
This meant that so long as there were 3 commas in each (using the example above), the validation would see the correct number of parts to each parameter, which is incorrect.

If any one can understand what I’m trying to do please can they suggest a better method of validation.
I’d like to make the script as water tight as possible for other users.

Thanks in advance.

Regards,

Nick

Browser: Safari 419.3
Operating System: Mac OS X (10.5)

Can you explain what problems you encountered with this method?

Hi James,

Thanks for your reply.

Basically, if I were to check the two parameters below they would produce the same answer, which is 4.

“W,A,D,E”
“,A,D,E”

As you can see the second line has no W, however it still has three commas.
If I were to set text item delims to a comma and check the number of items in each line the answer would be 4.
However, as you can see, in the second line there are only 3 letters A,D,E not 4.

Without having to dig right down I’m looking for a simple method to validate the number of parts to each parameter, each one being separated by a comma.

Hope that explains it a little better.

Regards,

Nick

Okay this is extraordinary ugly looking, but it does seem to work. Hopefully someone will have a cleaner and/or better idea.

set status to generateText("M,A,D,E", "<@interReg>,<@interReg>,<@interBold>,<@interReg>", "M,N,T,T")



on generateText(param1, param2, param3)
	set param1count to validateParams(param1)
	if param1count is false then
		return "Empty Value in param set 1"
	else
		set param2count to validateParams(param2)
		if param2count is false then
			return "Empty Value in param set 2"
		else
			if param2count is not equal to param1count then
				return "Too many/few params in set 2"
			else
				set param3count to validateParams(param3)
				if param3count is false then
					return "Empty Value in param set 3"
				else
					if param3count is not equal to param1count then return "Too many/few params in set 3"
				end if
			end if
		end if
	end if
	-- Do your actual REAL routine stuff here
	return "Text Generated: Okay"
end generateText

on validateParams(valParam)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
	set subParamList to text items of valParam
	set AppleScript's text item delimiters to atid
	if subParamList contains "" then
		return false
	else
		return (count subParamList)
	end if
end validateParams

Hi James,

Thanks for your reply and for taking the time to put together the code, it’s great and does exactly what it says on the tin!

Fantastic! :slight_smile:

Thanks James.

Best Regards,

Nick

P.S. Am I right in thinking it’s checking each part of the parameter to see if it contains something?

You are correct. Using text item delimiters it separates each parameter set into a list of text items. If any of the text items are “” (ie there was a coma with nothing associated) it throws an error. Otherwise it uses the first parameter set as the baseline and throws an error if either set 2 or 3 contains more or less sub parameters than set 1.

Glad it works for you!

Hi James,
Thanks for the explanation.
Thanks again for your help with this, I’ve implemented your code and it works a treat. :slight_smile:

Best Regards,

Nick

Hey Nick, here is a more compact version that also allows you add additional sets of parameters without having to worry about modifying the validation code. The other nice thing is now all of the actual validation code happens in the validate handler rather than having part of it live in your generateText handler.

set status to generateText("M,A,D,E", "<@interReg>,<@interReg>,<@interBold>,<@interReg>", "M,N,T,T")


on generateText(param1, param2, param3)
	tell validateParams({param1, param2, param3}) to if it is not true then return it
	-- Do your actual REAL routine stuff here
	return "Text Generated: Okay"
end generateText

on validateParams(paramList)
	repeat with i from 1 to (count paramList)
		set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
		set subParamList to text items of (item i of paramList)
		set AppleScript's text item delimiters to atid
		if subParamList contains "" then
			return ("Empty value in param set" & i)
		else
			if i is 1 then
				set subParamCount to (count subParamList)
			else
				if (count subParamList) is not equal to subParamCount then return ("Too many/few params in set " & i)
			end if
		end if
	end repeat
	return true
end validateParams

Let me know what you think!

Hi James,

Wow. The new version looks even better. :slight_smile:

It’s definitely more streamlined and as you say allows for more parameters to be passed in which is great.
I’ve not had chance yet to implement it in to the rest of the script but I will be.
I’ll have to remove the original version which was superceeded within the space of 3.5 hours!

Thanks again James for spending more time on this query, it has certainly added to my knowledge of Applescript and has introduced me to new concepts and ways of working.

I like this for sorting the text item delimiters, a lot more compact than the way I’ve been doing it.

set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}

I hope other people find the exercise as useful as I did.

Best Regards,

Nick

Glad you like it Nick and glad that I could be of some help to you!