Some Simple Functions4U

Back To LiveCode Main Page

Here are some simple functions for you...


isDiff()

I know you can use the diffCompare command to compare two strings. It will return a list representing the changes between two text strings but sometimes you just need a simple true or false response. isDiff will return a true if the two strings are different and a false if both are exactly the same.

So if you were to pass "string" and "String" to this function it will return True for being different.

function isDiff str1 str2
set the caseSensitive to true #Turn case-sensitive on
if str1 is str2 then # Compare the two strings
return false # If they are the same then return false
else
return true # If they are NOT the same then return true
end if
end
isDiff

Example of the following code in a button calling isDiff()

on mouseUp pMouseButton
put isDiff("string","String") into tmp
answer tmp titled "isDiff Example"
end mouseUp

produced the following result. True that "string" and "String" are different.

isLetter()

There is a function named isNumber() in LiveCode. It will verify if the character passed to it is a number or not. The function isLetter will accept a single character and like isNumber verifying a number, isLetter will verify if the character is a letter or not.

function isLetter toTest
put "abcdefghijklmnopqrstuvwxyz" into alphabet #Build alphabet string

if number of chars in toTest > 1 then # If toTest has more than 1 char...
put char 1 of toTest into toTest #...then reduce it to the first char.
end if

put offset(toTest,alphabet) into alphaOffset # Check if toTest is within alphabet
if alphaOffset > 0 then # If toTest was found in the alphabet string then...
return true #...return true to code that called this function, but...
else
return false #...if it wasn't found then return false.
end if
end
isLetter

isAlpha()

This is a function that tests each character in a string to determine if it is an alphabetical letter. If all characters are alphabetical characters, then it returns true. If the string is tainted by a non-alphabetical character such as a number or symbol then it returns a false.

function isAlpha toTest
## Determines if string is all letters ##
put "abcdefghijklmnopqrstuvwxyz " into alphabet #Build alphabet string. Notice the space after z.

repeat with i = 1 to number of chars in toTest # Start looping all characters in toTest
put char i of toTest into testChar # Select each single character to test
put offset(testChar,alphabet) into alphaOffset # Check if to testChar is within alphabet
if alphaOffset is 0 then
exit
repeat # A non-letter was found within string. Exit the loop.
end if
end
repeat

if
alphaOffset > 0 then # If toTest was not found to be purely alphabetical then ...
return true #...return true to code that called this function, but...
else
return false #...if it was purely alphabetical then return false.
end if
end
isAlpha


isSymbol()

This function like the isLetter() function will return a true if the character is a symbol and a false it it isn't. It works the same as the isLetter() function.

function isSymbol toTest
put "~`!@#$%^&*()_-+=;:'<,>.?/" & quote into symbols # Build symbols string
#
Notice you can expand the above string to include more symbols. I only put the common...
# ...ones found on my keyboard.

if number of chars in toTest > 1 then # If toTest has more than 1 char...
put char 1 of toTest into toTest #...then reduce it to the first char.
end if

put offset(toTest,symbols) into symbolOffset # Check if toTest is within the symbol list
if symbolOffset > 0 then # If toTest was found in the symbols string then...
return true #...return true to code that called this function, but...
else
return false #...if it wasn't found then return false.
end if
end
isSymbol

quote2()

function quote2 str2quote
return quote & str2quote & quote # Place a quote before and after string before returning it.
end quote2


This function make it easy to enclose text within a string with quotes. For example the following

answer quote2("Nevermore") & " said the raven."

will produce the following result...

quote1()

This function does the exact same thing as the quote2() function but with single quotes. Single quotes are often used to quote material within a quote. This can be a sentence or a command being passed to the shell.

function quote1 str2quote
return "'" & str2quote & "'" # Place a quote before and after string before returning it. Note it is a single ' enclosed by a "
end quote2

Example:

answer quote & "I just read " & quote1("Jabberwocky") & " and had to memorize it" & quote

will produce the following result...


ohmsLaw(v,i,r)

This function calculates the relationship between voltage, current and resistance in an electrical circuit. Depending on which parameter variable is empty; (volts, amps, or ohms) will determine which one is being determined.

function ohmsLaw v i r
# v is Volts, the unit of measurement for the force
# i is Amps
, the unit of measurement for the current. The reason for i is for the French phrase...
#...intensité du courant, (current intensity). It was first used in 1820 by André-Marie Ampère, ...
#...after whom the unit of electric current is named.
#
r is Ohms, the unit of measurement for resistance.


if v is empty then
#Find Volts by multiplying amps and ohms
return (i*r)
end if

if i is empty then
#Find Amps by dividing voltage by ohms
return (v/r)
end if

if r is empty then
#Find Ohms by dividing voltage by amps
return (v/i)
end if
end ohmsLaw