       RE: Checking the form of an option

• To: mathgroup at smc.vnet.net
• Subject: [mg20997] RE: [mg20982] Checking the form of an option
• From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
• Date: Thu, 2 Dec 1999 21:41:11 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Mark Diamond wrote:
----------------------------
I want to check whether option "K" to a function is an integer, a list of
single numbers, or neither of these, and then to execute one of three
different statements.

If these K were a simple parameter, I could do it with
f[k_Integer] := functionBody;
f[k:{_Integer}..] := functionBody;
f[k_] := functionBody

but I cannot see how to check for similar conditions on an *option* inside
one function. Can anyone help?
---------------------------------

There are many ways to approach this.
I write a toy example that does what you want.

Notice the last pattern in Switch is (_) which matches anything. You only
get the that pattern if the other two fail.

In:=
Options[f] = {K -> 2};

f[x_, opts___] :=
With[
{k1 = K /. Flatten[{opts, Options[f]}]},
Switch[k1,
_Integer, {k1, "K is an Integer."},
{__Integer}, {k1, "K is a list of Integers."},
_, {k1, "K is neither an Integer or list of Integers."}
]
]

----------------------
Now I work some examples. The first one uses the default setting K->2

In:=
f[x]

Out=
{2, "K is an Integer."}

In:=
f[x, k -> 5]

Out=
{2, "K is an Integer."}

In:=
f[x, K -> {2, 3}]

Out=
{{2, 3}, "K is a list of Integers."}

In:=
f[x, K -> 2.3]

Out=
{2.3, "K is neither an Integer or list of Integers."}

--------------------
Regards,
Ted Ersek

For Mathematica tips, tricks see
http://www.dot.net.au/~elisha/ersek/Tricks.html

```

• Prev by Date: Re: date/time ListPlot
• Next by Date: Re: date/time ListPlot
• Previous by thread: Checking the form of an option
• Next by thread: Re: Checking the form of an option