REPLACE Doesn't. - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: REPLACE Doesn't. (/thread-8073.html) |
REPLACE Doesn't. - toml_12953 - 03-31-2017 01:40 PM I'm trying to replace spaces in a string with zeroes but I get the same string back unchanged. The program below prints " 9" " 9" Of course I'm doing something wrong but I can't figure out what. REPLACE does work at the home screen. Tom L PHP Code: EXPORT test() RE: REPLACE Doesn't. - Dieter - 03-31-2017 01:48 PM (03-31-2017 01:40 PM)toml_12953 Wrote: This probably is a very dumb answer, as I do not even have a Prime at hand to check it. But in all programming languages I know the "replace" string function does not replace the i–th character, instead it replaces one substring by another. Like this: replace("apple pie", "apple", "orange") => "orange pie" So the right approach may be something like a simple replace(x," ","0"). No for-loop required. But again: probably this is a very stupid answer. You should know since you've got the manual. ;-) Dieter Edit: replaced a 9 with a 0. RE: REPLACE Doesn't. - eried - 03-31-2017 01:53 PM You have to store the new result: replace(x,i,"0"); change to: x:=replace(x,i,"0"); RE: REPLACE Doesn't. - toml_12953 - 03-31-2017 02:06 PM (03-31-2017 01:48 PM)Dieter Wrote:(03-31-2017 01:40 PM)toml_12953 Wrote: I tried this: PHP Code: x:="apple pie"; "apple pie" prints. I'm really craving orange pie! It seems to be a problem replacing characters in a variable. Tom L RE: REPLACE Doesn't. - Han - 03-31-2017 02:10 PM (03-31-2017 02:06 PM)toml_12953 Wrote: I tried this: See eried's post prior to yours; you need to save the result since the function does not modify the input in place. RE: REPLACE Doesn't. - toml_12953 - 03-31-2017 02:15 PM (03-31-2017 02:10 PM)Han Wrote:(03-31-2017 02:06 PM)toml_12953 Wrote: I tried this: Ah! I didn't realize that even parameters in predefined functions are passed by value. Thanks! Tom L RE: REPLACE Doesn't. - Tim Wessman - 03-31-2017 03:59 PM (03-31-2017 02:15 PM)toml_12953 Wrote: Ah! I didn't realize that even parameters in predefined functions are passed by value. Thanks! Everything in PPL is passed by value. I believe the only exception here is that when you cross into the CAS inside a program, variables are passed as variables so that the CAS can do what it needs with them. RE: REPLACE Doesn't. - Dieter - 03-31-2017 05:25 PM (03-31-2017 02:15 PM)toml_12953 Wrote: Ah! I didn't realize that even parameters in predefined functions are passed by value. Does this mean that this... Code: x:="apple pie"; ...actually works on the Prime? And what about your original example and x:=replace(x, " ", "0") then? Dieter, just curious. ;-) RE: REPLACE Doesn't. - toml_12953 - 04-01-2017 12:25 AM (03-31-2017 05:25 PM)Dieter Wrote:(03-31-2017 02:15 PM)toml_12953 Wrote: Ah! I didn't realize that even parameters in predefined functions are passed by value. Yes! I can now have my orange pie. and I get 00009 with that replace. Tom L |