0
$\begingroup$

First of all, I know replace or plugging input of given function through command Replace[] or /.{x->1} etc.

Here I want to focus not just on plugging or replacing variables into numbers but just replacing some of the variable's names or deleting unnecessary operations to another.

First of all suppose I have sequences aa1,aa2,aa3,aa4, bb1,bb2,bb3,bb4, cc1,cc2,cc3,cc4, etc. and I have a function whose expression is some combination of particular sequences.

I want to replace the name of all my sequences i.e., aa1-> ab1, cc1-> cb1, etc

So for a given input $aa1bb1+aa2cc1$, after this replacement, this produces $ab1bb1+ ab2cb1$.

Using some editor like notepad, I can do this by find and research easily but have wonder whether a similar thing can do on Mathematica itself.

$\endgroup$
2
  • 2
    $\begingroup$ Storing information in the variable's name instead of in its contents seems like a bad idea to me. $\endgroup$
    – Roman
    Commented Jul 23, 2021 at 14:06
  • 1
    $\begingroup$ Are you looking for an IDE that can rename all variables in a program file for you? (E.g. plugins.jetbrains.com/plugin/7232-wolfram-language) $\endgroup$
    – Michael E2
    Commented Jul 23, 2021 at 15:11

2 Answers 2

1
$\begingroup$

If you are trying to do replacement in the front end, you can try something like

ac = Cells[CellStyle -> {"Input"}];
content = NotebookRead /@ ac;
newContent = content /. {"aa1" -> "ab1", "cc1" -> "cb1"};
Scan[NotebookWrite[ac[[#]], newContent[[#]]] &, Range@Length@newContent];

enter image description here

$\endgroup$
0
$\begingroup$
Clear["Global`*"]

rules = {aa1 -> ab1, aa2 -> ab2, cc1 -> cb1};

convert[x_] := ToExpression@StringJoin[
   StringReplace[ToString[x],
    Map[ToString, rules, {2}]]]

expr = aa1bb1 + aa2cc1;

convert@expr

(* ab1bb1 + ab2cb1 *)
$\endgroup$

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.