Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using two For loops. When the inner loop finishes, I don't want to print but I want to store the result from the inner loop in some variable so that I can call that variable in the future. That result will come in the form of a list.

For example, in the 1st iteration,it is returning some value, e.g. {2,4}. in the 2nd iteration,it is returning {3,4}.

But I don't want to print them every time. I want some variable to store {{2,4},{3,4}}. How to do that?

Here I am retrieving all the data except Null.

tab={{{"option1", "option3", "option4", "optio", "Null", 
        "Null"}, {"option2", "option5", "option6", "option7", "Null", "Null"}, 
         {"option", "op", "Null", "Null", "Null", "Null"}}};

For[j = 1, j <= Length[tab[[1]]], j++,
  For[i = 1, i <= Length[Rest[tab[[1, j]]]], i++,

If[TrueQ[(Rest[tab[[1, j]]][[i]]) == "Null"], count++, count = 0]]
Table[Rest[tab[[1, j]]][[i]], {i,1, (Length[Rest[tab[[1, j]]]] - count), 1}]] ]
share|improve this question
Please post a code sample – Dr. belisarius Aug 15 '12 at 11:37
Not only should you post a code sample, but since there is a high probability that a method better than For loops exists for your problem please also explain the context of the code. – Mr.Wizard Aug 15 '12 at 11:41
Usually, one could use AppendTo[] or the combination of Sow[]/Reap[], but without something more concrete, we can't be more helpful. – J. M. Aug 15 '12 at 11:43
I have posted my code sample. – Jennifer Aug 15 '12 at 12:03
I updated my answer with a direct modification of the For loop, but I highly recommend learning about &, @, #, etc.! – Mr.Wizard Aug 15 '12 at 12:13
up vote 4 down vote accepted

In light of your update, is this all you're trying to do?

Cases[#, Except@"Null"] & /@ tab[[1, All, 2 ;;]]

{{"option3", "option4", "optio"}, {"option5", "option6", "option7"}, {"op"}}

Specific references to understand this code for your convenience:
Part, Function, Slot, Map, Cases, Except

If for some reason you feel you must use For loops, you can substitute Sow and Reap for Print as follows:

  For[j = 1, j <= Length[tab[[1]]], j++,
   For[i = 1, i <= Length[Rest[tab[[1, j]]]], i++,
    If[TrueQ[(Rest[tab[[1, j]]][[i]]) == "Null"], count++, count = 0]
   Sow @ Table[
     Rest[tab[[1, j]]][[i]], {i, 
      1, (Length[Rest[tab[[1, j]]]] - count), 1}]
][[2, 1]]

{{"option3", "option4", "optio"}, {"option5", "option6", "option7"}, {"op"}}

share|improve this answer
DeleteCases[#, "Null"] seems clearer and avoids her learning Except yet, hehe – Rojo Aug 15 '12 at 15:15

A version using Select, Map ( shortform /@ ) and lambda functions ( # and & ).

Select[Rest@#, # =!= "Null" &] & /@ First@tab

{{"option3", "option4", "optio"}, {"option5", "option6", "option7"}, {"op"}}

I wasn't sure if tab was intended to have an apparently redundant layer of nesting, as in {{{...},{...},{..}}}, if it wasn't you can drop the extra braces and remove First from the code.

Did you also want to ignore the first element of each sublist of tab as per Mr Wizards answer? If not, then you can drop Rest from the code given.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.