Re: How to Return from within a Table[]-Command?
- To: mathgroup at smc.vnet.net
- Subject: [mg33751] Re: [mg33728] How to Return from within a Table[]-Command?
- From: "Johannes Ludsteck" <johannes.ludsteck at wiwi.uni-regensburg.de>
- Date: Thu, 11 Apr 2002 02:14:05 -0400 (EDT)
- Organization: Universitaet Regensburg
- Sender: owner-wri-mathgroup at wolfram.com
Dear Detlef, unfortunately you didn't tell exactly what you want: Do you want that Mathematica breaks if a non-integer is found without returning anything or do you want that Mathematica returns the list of transformed variables before the non-integer element. If you want the former, a possible solution is In[1]:= fun[li_List]:=Block[{t}, t=Catch[Scan[If[!IntegerQ[#],Throw[1]]&,li]]; If[t===1,"Vector invalid", Map[#/2&,li]]] i. e. first check whether the vector is valid and the apply #/2 If you want the latter, you could define a counter variable in the clock and count the number of elements until the non-integer is reached. Then Throw the value of the counter and use in in Map[#/2&,Take[li,counter]] to get the result. Or if you want to use standard package functions: In[1]:= <<Statistics`DataManipulation` In[2]:= fun[li_List]:= Map[#/2&,TakeWhile[li,IntegerQ]] In[3]:= fun[{1,2,a}] Out[3]= {1\/2, 1} best regards, Johannes On 10 Apr 2002, at 0:49, 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 > <><><><><><><><><><><><><><><><><><> Johannes Ludsteck Institut fuer Volkswirtschaftslehre Lehrstuhl Prof. Dr. Moeller Universitaet Regensburg Universitaetsstrasse 31 93053 Regensburg Tel +49/0941/943-2741