replace a character in a string

Hi,
If I have a string like
my_string_VALUE
and I want to make a loop on the value of VALUE from 0.0 to 0.99 while replacing the dot by the letter p (as I guess nobody wants a wave name with a dot), how can I proceed?

The result I need should be strings of form:
my_string_0p00 my_string_0p01 my_string_0p02... my_string_0p99

Thank you so much,
Have a look at ReplaceString(replaceThisStr, inStr, withThisStr  [, caseSense  [, maxReplace ] ])
It should do what you want.

HJ

Edit: You don't even need a loop:
Generate a dummy wave
make /T tst="asdf_0."+num2str(p)
Edit/K=1 'tst'

change the periods
tst=replacestring(".",tst[p],"p")

and have a look at the changes in the table
Great! Thanks, it seems to do exactly what I want.
though I think there is no need of [p] in the command you indicate:
 tst=replacestring(".",tst,"p")
seems to also work :)

Thank you for your quick and accurate help!
HJDrescher wrote:
Have a look at ReplaceString(replaceThisStr, inStr, withThisStr  [, caseSense  [, maxReplace ] ])
It should do what you want.

HJ

Edit: You don't even need a loop:
Generate a dummy wave
make /T tst="asdf_0."+num2str(p)
Edit/K=1 'tst'

change the periods
tst=replacestring(".",tst[p],"p")

and have a look at the changes in the table


This doesn't quite work because it give 0.0, 0.1, 0.2 ... 0.10 etc
 Make /T tst="asdf_" + num2str(p/100)
Is closer to what you want, although the first point is still not right.

You can also do this in a loop and pad the variable in the string so that 0.01 to 0.09 come first.
HJDrescher is right on the usage of replacestring if you have already written the string variable/ wave.

If you insist on creating the string within a loop, I think the best will be to just concatenate strings, i.e.:

string s_value
string s_prefix = "my_string_0"

variable v_value, v_max = 100
for (v_value=0; v_value<v_max; v_value+=1)
    s_value = s_prefix +  replacestring(".", num2str(v_value/100), "p")
endfor


But the above code will search _every time_ for the "." position. Chances are it is never going to be different. Not ideal.

So this can be optimized. But this will depend on the value.
For example, if the value is in the interval [0-100) (as you showed), I would use:
 
s_prefix = "my_string_0p"
for ..
    s_value = s_prefix + num2str(v_value)
endfor ..


You can take a look at sprintf for formatted printing to a string.

As to "doing it without a loop", using make: I think make uses a loop itself. It populates (hopefully efficiently) the waves with some sort of a loop using the dimension provided (128 if not provided for rows; ignored for other dimensions), i.e.:
make/o/n=(10,100) wave0 = p+q

where p and q are the loop variables. And there are different loops for different loop variables. I might as well be wrong here though.

best,
_sk
Are you doing set of individual strings (e.g. a set of waves)? Then, you need a loop with one or the other of the methods mentioned.

Are you doing a string list? Then, you can use ReplaceString without a loop. With VALUE as 0.0 to 0.99, just to replace the dot, use "." to "p" as ...

ReplaceString(".",stringList,"p")


Example

print ReplaceString(".","my.day;today.day;the.0.day;","p")
  mypday;todaypday;thep0pday;


I might suggest to replace the dot by "_" (underscore) or "-" (dash) rather than p when these are filenames.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
jjweimer wrote:

Are you doing a string list? Then, you can use ReplaceString without a loop. With VALUE as 0.0 to 0.99, just to replace the dot, use "." to "p" as ...

jjweimer wrote:

Example

print ReplaceString(".","my.day;today.day;the.0.day;","p")
  mypday;todaypday;thep0pday;



This list will still need to be unpacked to individual wave names with, for example, stringfromlist(i, "mypday;todaypday;thep0pday", ";"), where i is the loop variable. So a loop is again needed.

Loops are not bad ;-).

best,
_sk
jjweimer wrote:
Are you doing set of individual strings (e.g. a set of waves)? Then, you need a loop with one or the other of the methods mentioned.

Are you doing a string list? Then, you can use ReplaceString without a loop. With VALUE as 0.0 to 0.99, just to replace the dot, use "." to "p" as ...

ReplaceString(".",stringList,"p")


Example

print ReplaceString(".","my.day;today.day;the.0.day;","p")
  mypday;todaypday;thep0pday;


I might suggest to replace the dot by "_" (underscore) or "-" (dash) rather than p when these are filenames.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH


Actually this is what I finally did :)
tst=replacestring(".",tst,"p")
Thanks for your help!

Try this:

make /T tst="a"+num2str(0.01*p+0.001)+"s"

You get:

a0.001s
a0.011s
a0.021s
a0.031s
a0.041s
a0.051s
a0.061s
a0.071s
.....

Then

tst=replacestring(".",tst,"p")

You get:

a0p01s
a0p11s
a0p21s
a0p31s
a0p41s
a0p51s
a0p61s
a0p71s
...

Then

tst=replacestring("1s",tst,"")

You get:

a0p00
a0p01
a0p02
a0p03
a0p04
a0p05
a0p06
a0p07
...