Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

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.