Re: How to Return from within a Table[]-Command?

• To: mathgroup at smc.vnet.net
• Subject: [mg33763] Re: [mg33728] How to Return from within a Table[]-Command?
• From: Sseziwa Mukasa <mukasa at jeol.com>
• Date: Thu, 11 Apr 2002 02:14:27 -0400 (EDT)
• References: <200204100449.AAA20770@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Detlef Mueller wrote:

> Hello,
>
> I wanted to scan trough al List, building a new
> List, but aborting, if one Element of the first
> List violates a special Condition ...
> say for example:
>
> Divide2[L_List] := Module[{},
>     Table[
>       If[IntegerQ[L[[i]]],
>         i/2,
>         Return["vector not valid."];
>         ]
>       , {i, 1, Length[L]}
>       ]
>
>     ...
>
>     ]
>
> But this results:
>
> In[13]:= Divide2[{2,3,a}]
> Out[13]= {1/2, 1, Return["vector not valid."]}
>
> Wich is of course not what I want.
>
> Greetings
>
>   Detlef

Do you really need an error message?  You can use Abort for that, but if
all you want to do is process until a condition occurs use recursion
divide2[l_List]:=If[l=={},{},Block[{f=First[l]},If[IntegerQ[f],Flatten[{f/2,divide2[Rest[l]]}],{}]]].
With Abort it becomes:

divide2::"invalid" = "Vector must be all integers";
divide2[l_List]:=If[l=={},{},Block[{f=First[l]},If[IntegerQ[f],Flatten[{f/2,divide2[Rest[l]]}],Message[divide2::"invalid"];Abort[]]]]

I suppose you can use Catch and Throw if you can figure out a reasonable
value to Throw back.  If all you want to do is check if the list didn't
contain only integers use the VectorQ function

divide2[l_?(VectorQ[#,IntegerQ[#]&]&)]:=l/2

which won't process a list unless it contains only integers.

Regards,

Sseziwa

```

• Prev by Date: Problems with creating packages (formatting)
• Next by Date: Re: Question regarding Compile
• Previous by thread: How to Return from within a Table[]-Command?
• Next by thread: RE: How to Return from within a Table[]-Command?