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

This question already has an answer here:

I'm new to mathematica so I hope this is not a too stupid question, but I couldn't find a solution in the mathematica documentation.

I've got a normal table, initialized with "0" as entries. In the first element, I want to put a List of few numbers. I tried to build a minimalistic example as follows:

visitedNodes = Table[0,{i,1,5}]
visitedNodes[[1]] = List[1,2,3]

so visitedNodes = {{1,2,3},0,0,0,0}. Now I test:

For[i=1, i <= Length[visitedNodes], i++,
    If[visitedNodes[[i]] != 0, 
        a = 5;   (* like: "do something" *)
        Print["I'm in!"];

Since it's a "List Object", for me it's != 0, but guess I have to test it on somewhat else... but on what? :) Thank you a lot, this site also helped me already a lot =).

Edit: The question is, that the If statement for i=1 should be like "Yes, a List[1,2,3] is not equal 0, since it's the first element of visitedNodes", but it does not. So why is it like that?

share|improve this question

marked as duplicate by R. M. Jan 3 '14 at 23:29

This question was marked as an exact duplicate of an existing question.

up vote 0 down vote accepted

Use visitedNodes[[i]] =!= 0

and check Unequal vs UnsameQ.

share|improve this answer
=!= works, thanks a lot :) Edit: Oh, I see the difference between Unequal vs. UnsameQ. Didn't knew about UnsameQ :). Thanks! – user1706465 Jan 3 '14 at 23:21

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