If-statement does not evaluate if condition is true [duplicate]

I know there are other ways to solve this problem. Just because I'm curious I tried:

Map[If[# == 1, , #] &, {2, 1, 3, 1, 4}]


This should return a list without the value 1, because when the statement is true it should do nothing. The solution should be:

{2,3,4}


and not

{2,Null,3,Null,4}


Why is it not working as I expect?

-
thank you ssch. I know that this works. Just wanted to know if there is a possibility to get the solution out of the if directly. – Frink Dec 21 '12 at 10:06
Map[If[# == 1, Unevaluated[Sequence[]], #] &, {2, 1, 3, 1, 4}] in that case :) – ssch Dec 21 '12 at 10:08
I do not know if this the same as @ssch above, but this is what I normally write in these cases : Map[If[# == 1, Sequence @@ {}, #] &, {2, 1, 3, 1, 4}] – Nasser Dec 21 '12 at 10:27
Frink, please try to make an effort to find existing questions which address your problem before asking a new one. In this case a search for "If Null" would have brought up the proper question as the first result. – Mr.Wizard Dec 21 '12 at 15:03

marked as duplicate by Mr.Wizard♦Dec 21 '12 at 14:59

Your If statement is actually

If[# == 1, Null, #]


so it is working perfectly. Try evaluating

Hold@Map[If[# == 1, , #] &, {2, 1, 3, 1, 4}]


to see for yourself. To get what you want, try

Map[If[# == 1, Unevaluated@Sequence[], #] &, {2, 1, 3, 1, 4}]

-
thx, exactly what I was looking for :-) – Frink Dec 21 '12 at 10:22
Maybe mentioning If[x > 1, , 2] // FullForm, which evaluates to If[Greater[x, 1], Null, 2], is also useful – acl Dec 21 '12 at 10:31
Incidentally, Unevaluated@Sequence[] can be replaced with ##&[] which I call a "vanishing function." – Mr.Wizard Dec 21 '12 at 15:01
@Mr.Wizard. ##&[] is a neat trick! – m_goldberg Dec 21 '12 at 17:21