MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33766] Re: How to Return from within a Table[]-Command?
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Thu, 11 Apr 2002 02:14:54 -0400 (EDT)
  • References: <a90goo$kdb$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Detlef,
Return only returns from certain constructs, and Table is not one of these.
I suggest using Throw and Catch:

    Divide2[L_List]:=
      Module[{},
          Table[If[IntegerQ[L[[i]]],i/2,Throw["vector not valid."]],{i,1,
              Length[L]}]
      ]//Catch

    Divide2[{2,3,a,5}]


        vector not valid.

The story on Return is

If Return[x] is generated as a value in  Do or Scan then  x is immediately
returned;
If  Return[x] is generated as an entry in CompoundExpression or as a value
of the body of a While or For loop then  Return[x] (not x) is immediately
returned;
If  Return[x]  is generated as the value of a user-defined function then the
function returns  x (not Return[x])
Otherwise Return[x] behaves as a ordinary expression.

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Detlef Mueller" <dmueller at mathematik.uni-kassel.de> wrote in message
news:a90goo$kdb$1 at smc.vnet.net...
> 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
>




  • Prev by Date: Re: Trouble with inverting matrices
  • Next by Date: Re: How to Return from within a Table[]-Command?
  • Previous by thread: RE: How to Return from within a Table[]-Command?
  • Next by thread: Re: How to Return from within a Table[]-Command?