12
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ The warning appears because you are trying to assign 2 to _. There are a lot of ways to do what you need: {i, j} = Drop[{1,2,3}, {2}] for example. $\endgroup$ – Kuba Aug 9 '13 at 10:15
  • 1
    $\begingroup$ It should be noted that often this type of situation is easily avoidable by instead doing something like: {i,j}={1,2,3}[[{1, 3}]]. $\endgroup$ – jVincent Aug 9 '13 at 13:01
14
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I just got a new favorite dummy symbol! And very interesting answer too! $\endgroup$ – Pinguin Dirk Aug 9 '13 at 11:05
  • $\begingroup$ @Pinguin Thanks. I think [Esc] :-@ [Esc] is a nice option. :^) $\endgroup$ – Mr.Wizard Aug 9 '13 at 11:15
  • $\begingroup$ good thing they "announced" retina support for the Mac, that'll look great in the code $\endgroup$ – Pinguin Dirk Aug 9 '13 at 11:26
1
$\begingroup$

The equal sign is trying to assign the list {1,2,3} to the left hand side {i,_,j} and you cannot assign 2 to _.

Try the following code:

list = {a, b, c};
Extract[list, {{1}, {3}}]
{First[list], Last[list]}
Replace[list, {i_, _, j_} :> {i, j}]
$\endgroup$
  • 2
    $\begingroup$ Thanks. I was aware of the more verbose syntax for Extract which will certainly work, but I was hoping there was a more natural, concise syntax similar to that which I suggested? $\endgroup$ – fhusb Aug 9 '13 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.