Skip to main content

list manipulation - Append in For loop does not work


I need to break my for loop in case of an thrown message and append s to the result list. Breaking the loop works, but my result is empty. I can not use Table[] because it does not support Break[].


EDIT: s has the form of list, so I get a Table structure for result


EDIT 2: AppendTo[] yields empty result too.


result = {};
For[i = 0, i < 6, i++,
s = {Quiet[
Check[If[i == 3, Message[FindRoot::jsing, x, 1], i], "Nan",
FindRoot::jsing]]};

If[s == "Nan", Break[], AppendTo[result, s]];
]
result

yields:


{}

Any help appreciated.



Answer



How about:



result = {};
For[i = 0, i < 6, i++,
s = Check[If[i == 3, Message[FindRoot::jsing, x, 1], i], "Nan",
FindRoot::jsing];
Print[s];
If[s == "Nan", Break[], result = {result, s}];]
Flatten[result]

or


result = {};

For[i = 0, i < 6, i++,
s = Check[If[i == 3, Message[FindRoot::jsing, x, 1], i], "Nan",
FindRoot::jsing];
Print[s];
If[s == "Nan", Break[], AppendTo[result, {s, dim}]];]
result

Comments