3
$\begingroup$

I have some variables derived in StringForm. For example;

variables = StringForm["x=``,\[Delta]=``", 5, 10]

How to substitute the variables to an expression/function.

expr=\[Delta]*x];

 With[{variables}, expr//Evaluate]

When I evaluate the code, I get the following error: enter image description here

How to solve the error, I want to get 50 as follows:

enter image description here

I think we should transform variables to a value (or plain ).

$\endgroup$
3
  • 1
    $\begingroup$ Why do you want to pass parameters as a String? I think there are better options like Association, or others. $\endgroup$
    – rhermans
    Commented Jan 5, 2022 at 14:29
  • 2
    $\begingroup$ I agree with @rhermans: this looks like a classic XY problem. $\endgroup$
    – Roman
    Commented Jan 5, 2022 at 15:25
  • 1
    $\begingroup$ The OP is likely asking for help in a way that obscures the real issue. We have seen long good answers that are likely to bring the OP in the wrong direction. $\endgroup$
    – rhermans
    Commented Jan 5, 2022 at 15:56

2 Answers 2

4
$\begingroup$

It was a learning experience.

enter image description here


With[{v = variables},
 Echo[v, "v="];
 Echo[ToString@v, "string @v="];
 Echo[StringSplit[ToString@v, ","], "SSp @v="];
 ToExpression@StringSplit[ToString@v, ","];
 Echo[x, "x="];
 Echo[\[Delta], "\[Delta]="];
 expr = x \[Delta]
 ]
$\endgroup$
3
$\begingroup$

It is not quite clear, what do you want to achieve. It is always helpful if in the question one formulates the result he needs to get.

I understood the question such that you want to transform the StringForm into the expression. Then after the evaluation Mma will know that x is 5 and Delta is 10. Is it right? If yes, try the following.

Step 1: let us form two strings

str1 = StringTake[ToString[variables], 3];
str2 = StringTake[ToString[variables], 5 ;; 8];

So far, str1 and str2 are strings

str1
str2

(*  "x=5"

"\[Delta]=10"  *)

One can check it:

Head[str1]

(*  String  *)

Step 2:
Now, it is easy to transform them into expressions:

ToExpression[str1]
ToExpression[str2]

(*  5

10  *)

Let us check if Mma "knows" the values of x and Delta:

x
\[Delta]


 
   (* 5
    
    10  *)

Edit

Step 3:

expr = \[Delta]*x;
With[{x = ToExpression[str1],
  \[Delta] = ToExpression[str2]}, expr]

(*  50  *)

Done.

Some explanations:

  1. The construct With requires the Set statement within the curly braces. That is, With[{expr1=something1, expr2=something2}, expr3]. Your variable variables does not have a form of the Set statement. This is reported to you as an error message.

  2. The variable variables has a head StringForm. Please check Head[variables]. It is a specific form to work with texts. Using variables with such a head for standard calculations is possible (as I have shown) but strongly complicates the code. I generally recommend another way around. That is, making calculations in a usual way with standard variables, and then transforming these variables into strings when needed.

Hope it helps. Have fun!

$\endgroup$
2
  • $\begingroup$ Thank you for your interest. I edited the post. How to solve the error: With::lvws: Variable variables in local variable specification {variables} requires a value. $\endgroup$
    – HD239
    Commented Jan 5, 2022 at 11:35
  • $\begingroup$ @HD239 Please see the edits. $\endgroup$ Commented Jan 5, 2022 at 14:16

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.