Hi Tim,

> This appears to work:
> CharsAccepted: func [acceptable[string!] submitted[string!] /local
> inter-set exc-set]
> [
>   inter-set: intersect acceptable submitted
>   if (length? inter-set) <> (length? submitted)
>     [make error! rejoin[exclude submitted inter-set " are not acceptable
> characters"]]
>   return true
> ]

Alternatively, you can try using the difference/only function.  This
will return a series containing elements that are in the first, but not
the second series, ie. any characters in submitted that are not in
acceptable.  If the exc-set string is empty, no unacceptable characters
were found.

CharsAccepted: func [
  acceptable [string!] submitted [string!] /local exc-set
][
  either empty? exc-set: difference/only submitted acceptable [true]
    [make error! join exc-set " are not acceptable characters"]
]

And here is another example, a bit more advanced.  If the exc-set is
empty, the any function will return true immediately.  Otherwise it goes
on to return an error.  I've also put a '[catch]' in the function spec,
this means instead of the error occuring inside the CharsAccepted
function, it will occur at whereever the CharsAccepted function was
called.

CharsAccepted: func [[catch]
  acceptable [string!] submitted [string!] /local exc-set
][
  return any [empty? exc-set: difference/only submitted acceptable
    make error! join exc-set " are not acceptable characters"
  ]
]


An example -

>> test: func [] [CharsAccepted "abc" "abf"]
>> test
** User Error: f are not acceptable characters.
** Where: CharsAccepted "abc" "abf"

Without the [catch] attribute, this is what happens -

>> test
** User Error: f are not acceptable characters.
** Where: return any [empty? exc-set: difference/only submitted
acceptable
    make error! join exc-set " are not acceptable characters"
]

In a small function like CharsAccepted, using the [catch] attribute has
a small advantage, but it can probably help a lot in error handling with
larger functions.

Good luck :),
Julian Kinraid

Reply via email to