How to ignore list elements when extracting with pattern matching
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 Escddg
Esc 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.