I'm writing a program that will solve the value of 1 variable in a 4-variable equality equation, but the variable to be solved will depend on the user's input to the program.

This is what I've come up with: Create a Mathematica script that reads in 4 parameters from the commandline. Read in the given 4 values and assign it to 4 variables. The variable that needs to be solved will have a specific value (say -100). I need to identify that variable and Remove it from the Mathematica namespace, so that 3 variables remain.

I can then call the Solve function that contains n variables, but since I've used Remove on 1 of those, Mathematica solves the value for that variable.

The only part that I don't get to work, is to identify which symbol's value is -100 so that I can call Remove on the symbol.

Can anyone suggest a solution? I've tried putting all my variables in a list and find the symbol with value -100 using Position or Case, but it only returns the -100 numerical value and not the symbol name. As so:

(* Values for a, b, c and d have been read from the commandline using *)
(* a = ToExpression[$CommandLine[[4]]] etc  *)

list = {a, b, c, d}
(* {1, 2, 3, -100} *)

Position[list, -100]
(* {{4}} *)

Cases[list, -100]
(* {-100} *)

(* I can't call Remove on the answers given by Position and Cases to remove variable d *)

I've tried some other solutions as well but with no luck. In case it's relevant - I'm new to Mathematica.

  • $\begingroup$ Can you show us the code you've come up with so far? People will be able to give you much better suggestions that way. $\endgroup$
    – mfvonh
    Jun 24, 2014 at 10:40
  • $\begingroup$ As I understand your question, it would be better to call Clear on the variable you want to be value-free than to call Remove. $\endgroup$
    – m_goldberg
    Jun 24, 2014 at 12:34

3 Answers 3


Here's one way of doing this:

(* names of variables *)
syms = {"a", "b", "c", "d"}

(* values (these would be from user input in your case) *)
vals = {1, 2, -100, 3}

(* value *not* to assign to a symbol *)
noset = -100

(* clean out our symbols to be safe *)
Remove @@ syms

(* do the appropriate assignments *)
MapThread[If[#2 != noset, Evaluate[ToExpression[#1]] = #2] &, {syms, vals}];

(* what's there? *)
{a, b, c, d}

(* {1, 2, c, 3} *)

(* use them in some solve... *)
Solve[a + b == c + d]

(* {{c -> 0}} *)
syms = {a, b, c, d};
vals = {1, 2, -100, 3};

Table[If[vals[[i]] != -100, syms[[i]] = vals[[i]], syms[[i]]], {i, 1, Range@Length@syms}]

{1, 2, c, 3}


I'd suggest something like...

Block[{a, b, c, d}, 
  With[{a = 1, b = 1, c = 1}, 
    Solve[a + b + c + d == 0, First[Cases[{a, b, c, d}, _Symbol]]]]]

Block localizes the symbols. With gives some of them values. The First[Cases... finds the one without a value.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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