If I have a simple list, say {1, 2, 3}, and I want to extract a subset of the elements, what is the correct syntax? e.g.
{ i, _, j } = {1, 2, 3}
I want i = 1 & j = 3 (which works), but I get the following warning:
Set::nosym: _ does not contain a symbol to attach a rule to.
so clearly I am using the wrong placeholder for elements I wish to ignore.
Answer
Turn off the warning
It should be noted that we can treat Set::nosym as a warning message rather than an error, and simply turn it Off:
Off[Set::nosym]
{i, _, j} = {1, 2, 3};
{i, j}
{1, 3}
throw-away Symbol
You could asko designate a Symbol for this purpose as a kind of dev/null, using e.g. $Post to clear it every time. I'll pick \[DoubleDagger], entered EscddgEsc and displayed as ‡:
$Post = ((‡ =.; #) &);
Now you could make your assignment:
{i, ‡, j} = {1, 2, 3};
The value of ‡ is cleared after each evaluation so as not to take up memory.
For reference, the definition of $Post above is not entirely neutral. For example, by default entering Sequence @@ {1, 2, 3} will return Sequence[1, 2, 3] whereas with the definition it will return 1. The ugly but proper definition would be something like:
$Post = Function[x, ‡ =.; Unevaluated@x, HoldAllComplete];
Since $Post is only one way to clear the Symbol I didn't want to clutter the top of the answer with this code. Other methods would be RunScheduledTask, CellEpilog, etc.
Comments
Post a Comment