Message Boards Message Boards

0
|
2611 Views
|
3 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Replacing Variables from a big piece of code

Posted 10 years ago
Hi, again

Today I have a question which might be useful to innacutate "long lines of code" writers or simply lazy programmers emoticon.

Say, after writing a good part of a program we realize that we have named a variable unfortunately and we would like to change its name.

How do we proceed in this case?
Soppose we have the following piece of code and we want to replace dS with SS:
a = (1 - \[Theta])/(2 dS) (\[Mu] - \[Sigma]^2/dS);
b = 1/dt + (1 - \[Theta]) (r + \[Sigma]^2/dx^2);
c = (1 - \[Theta])/(2 dx) (-\[Mu] - \[Sigma]^2/dx);
\[Alpha] = -(\[Theta]/(2 dx)) (\[Mu] - \[Sigma]^2/dx);
\[Beta] = 1/dt - \[Theta] (r + \[Sigma]^2/dx^2);
\[Gamma] = \[Theta]/(2 dx) (\[Mu] + \[Sigma]^2/dx);
*the content is irrelevant

There is the option to convert it into String and then use StringReplace, but I was wondering if there are smarter ways.

Thank you, 
Sandu
POSTED BY: Sandu Ursu
3 Replies
Posted 10 years ago
This is perhaps equally dangerous as doing string replace

1. Scrape over the variable name in question to highlight it.
2. Hit <ctrl>C
3. Hit <ctrl>F
4. If this method is going to work the FindAndReplace menu should have the variable name in the Find: box.
5. Type the replacement name in the Replace With box.
6. Click Replace All.
7. Click the Yes you really mean it.

Use that carefully, perhaps by making appropriate backups so you can more easily recover from disasters.

I believe I have at times in the past had this refuse to correctly deal with some special characters or strings
and had just given up assuming that those were not possible, but I cannot come up with an example at the moment.
POSTED BY: Bill Simpson
Posted 10 years ago
You can position the cursor in the file, perhaps before or after opening the replace window, and then click Find repeatedly and then Replace And Find Next as appropriate.

Unfortuately Mathematica and computers in general lack the "do what I mean" button that magically fixes exactly what we want done and nothing else.

I suppose, if you know how to use it, you can open up something like the vi/ex editor on the saved notebook file and tell it to replace all matching strings between line nnn and mmm. When manually editing a notebook outside of Mathematica you need to delete the cached information in the Mathematica file, you can find that section by scanning through the file, and then save the resulting file. Be sure you test this method on a test file before you accidentally destroy hours or years of work on your real file.
POSTED BY: Bill Simpson
Posted 10 years ago
Forgot about, <ctrl>F...

I was thinking to apply the reaplacing only to a specific selection (and keep the "variable"(s) in other parts of the prorgam).. but I guess I can simply copy it in another nb and do the Replace All "surgery" there and then copy it back.

Thank you, Bill!!
POSTED BY: Sandu Ursu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract