3
$\begingroup$

I have about 1000 variable names. Four example are:

DPI[1]
PXAC[a, c]
TRANS
PDS[c]

I like to insert 0 in a specific position in the given variable names such as:

DPI0[1]
PXAC0[a, c]
TRANS0
PDS0[c]

I searched for solutions from the already asked questions, but could not find any. Normal insertion codes in this forum do not help me because in my case the insertion should be as shown above.

Two rules should be applied:

  1. insertion should always be before this [.
  2. for those variable names without [...], the insertion should be at the end of the word.

Thanks.

$\endgroup$
4
$\begingroup$
list = {DPI[1], PXAC[a, c], TRANS, PDS[c]}; 

Replace[list, 
  {h_[a__] :> Symbol[SymbolName[h] <> "0"][a], s_ :> Symbol[SymbolName[s] <> "0"]}, 
  {1}]
 {DPI0[1], PXAC0[a, c], TRANS0, PDS0[c]}
| improve this answer | |
$\endgroup$
  • $\begingroup$ It works fine for a newly created list of variables. In my specific case, the list is created out of the model equations by picking the relevant variables (by using another code that you have written a few months ago). Since my variables are associated with some model definitions, the current code does not work. I used ClearAll[ list ] to clean the variable definitions or any assignments but ClearAll[...]` is not cleaning anything. Any idea? $\endgroup$ – Tugrul Temel Apr 30 at 12:36
  • 1
    $\begingroup$ tried ClearAll["Global`*"]? $\endgroup$ – kglr Apr 30 at 12:48
  • $\begingroup$ Excellent...It worked as expected. Thank you very much. $\endgroup$ – Tugrul Temel Apr 30 at 12:50

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.