Hello,

I am working on something where i need to search and replace many variable names of a program.

For example,
I have a class Hello and its public variable is world, but the variable world has been changed to earth so now in the program i need to go and replace the world variable with earth. this is just one example but there are many different

variables to be replaced.
code .....
[B]Hello.world[/B] replace by 
[B]Hello.earth[/B] 
code ....

so far i have a 2D array with old variable name and new variable name

but i am not able to find the strings such as world since it is in a single word Hellol.world

Please help on how to look for world in AnyClassName.old_VariableName and replace it by AnyClassName.new_VariableName

Thank You...

Another option is to just change the definition, try compiling with a compiler, and look at the compiler errors.

Well i guess you can read each line of the file with getline functions and then search for each of your words inside the string. If it is found. you can use the erase function to remove that particular part of the string and then you can now insert your new word in its position.

I think you should post in more code so that we can help you out

Well i guess you can read each line of the file with getline functions and then search for each of your words inside the string. If it is found. you can use the erase function to remove that particular part of the string and then you can now insert your new word in its position.

No, that would be dumb -- you would end up breaking anything with the same name that belonged to another class.

I think I might have mislead you guys, the main thing is that what if this is not part of a code and a random paragraph, or something that i can not put in an IDE which lets me use those function such as renaming and stuff. How would I achieve this task then ?

Yes, but the string might contain of a substring where only the substring needs to be replaced. For example, in "Hello.World" being one whole string I only want to replace World with "Earth".

Sorry If I confused you,

Thanks

Yes, but the string might contain of a substring where only the substring needs to be replaced. For example, in "Hello.World" being one whole string I only want to replace World with "Earth".

What you described (only replacing the "World" part with "Earth") is equivalent to replacing the whole phrase "Hello.World" with the phrase "Hello.Earth".

There are plenty of tools that can do search and replace across a directory for you.

Obviously, but the word hello could be anything. All I care about is the word acted the "."

No, that would be dumb -- you would end up breaking anything with the same name that belonged to another class.

Well when thats what he wanted. I guess thats what we should give. Hence it isnt dumb in any way.


If i had to classes hello and world with member apple.

i would search for hello.apple but not only for apple. :)

Yes , that's the main thing there are so many classes that it is impossible to save them in an array but some variable names through these classes are same these variable names could be stored in an array. However, this is why it is necssecary to only look for the word "World" rather than the whole word "Hello.Wordl" and replace it with "Earth" resulting in "Hello.Earth".

that's the main thing there are so many classes that it is impossible to save them in an array but some variable names

We're getting somewhere. Is there a particular reason you need to write this in C++? Would a solution that doesn't use C++ suffice?

So just run something like

perl -pi -e 's/foo/bar/g' *.cpp *.h

in every directory. Or maybe something like

find . -name "*.cpp" | xargs perl -pi -e 's/foo/bar/g'

assuming you're running in a unix-like environment. I'm sure you could do something similar in Windows. (Note that 'find' is different on Windows.)

For tasks like this, C++ is not the ideal language.

I do not understand what you mean by the above post .. could you please explain?

This help would be Great....!!!
Kamal Joshi

I am talking about using commands like perl, xargs, and find, from the command line. You might need to install software if you want to use these on Windows.

Yes, I get what you mean, but can you give me an example ?

Thanks

Well, for example, install Perl, xargs, and some version of find on your computer, or just install cygwin, and run the example I gave you.

But that's DUMB. Most IDEs and many text editors have this functionality built in.

This article has been dead for over six months. Start a new discussion instead.