Suppose I wanted to write down a list of $p^2$, for $p$ a prime between $1$ and $20$. I would expect
Table[If[PrimeQ[k], k^2, Sequence[]], {k, 1, 20}]
to work. In fact, this produces,
{Null, 4, 9, Null, 25, Null, 49, Null, Null, Null, 121, Null, 169, Null, Null, Null, 289, Null, 361, Null}
Why does this happen, and is there a variant which does work the way I expect?
Of course, I can just do
Map[(#^2) &, Select[Range[20], PrimeQ]]
I don't have a real reason to avoid this, but it feels less readable to me.
UPDATE: Given the answers I'm seeing, I should point out that this is meant to be a toy example and the actual code involves boolean functions much messier than PrimeQ[], for which there is no analogue of Prime[] available. But Szabolcs answer is exactly what I was looking for, thanks!
Table[]:Table[k^2, {k, Select[Range[20], PrimeQ]}]also works well. – J. M.'s missing motivation Nov 13 '12 at 22:22