Posts: 2,212
Threads: 198
Joined: Dec 2013
03-31-2017, 01:40 PM
(This post was last modified: 03-31-2017, 01:41 PM by toml_12953.)
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() BEGIN local x,i; print(); x:=" 9"; print(""""+x+""""); for i from 1 to 4 do replace(x,i,"0"); end; print(""""+x+""""); END;
Tom L
Cui bono?
Posts: 2,397
Threads: 61
Joined: Dec 2013
03-31-2017, 01:48 PM
(This post was last modified: 03-31-2017, 05:28 PM by Dieter.)
(03-31-2017, 01:40 PM)toml_12953 Wrote: PHP Code: for i from 1 to 4 do replace(x,i,"0"); end;
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.
Posts: 746
Threads: 63
Joined: Dec 2013
You have to store the new result:
replace(x,i,"0");
change to:
x:=replace(x,i,"0");
Posts: 2,212
Threads: 198
Joined: Dec 2013
03-31-2017, 02:06 PM
(This post was last modified: 03-31-2017, 02:07 PM by toml_12953.)
(03-31-2017, 01:48 PM)Dieter Wrote: (03-31-2017, 01:40 PM)toml_12953 Wrote: PHP Code: for i from 1 to 4 do replace(x,i,"0"); end;
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," ","9"). No for-loop required.
But again: probably this is a very stupid answer. You should know since you've got the manual. ;-)
Dieter
I tried this:
PHP Code: x:="apple pie"; replace(x, "apple", "orange"); print(x);
"apple pie" prints. I'm really craving orange pie! It seems to be a problem replacing characters in a variable.
Tom L
Tom L
Cui bono?
Posts: 1,882
Threads: 78
Joined: Dec 2013
(03-31-2017, 02:06 PM)toml_12953 Wrote: I tried this:
PHP Code: x:="apple pie"; replace(x, "apple", "orange"); print(x);
"apple pie" prints. I'm really craving orange pie! It seems to be a problem replacing characters in a variable.
Tom L
See eried's post prior to yours; you need to save the result since the function does not modify the input in place.
Posts: 2,212
Threads: 198
Joined: Dec 2013
(03-31-2017, 02:10 PM)Han Wrote: (03-31-2017, 02:06 PM)toml_12953 Wrote: I tried this:
PHP Code: x:="apple pie"; replace(x, "apple", "orange"); print(x);
"apple pie" prints. I'm really craving orange pie! It seems to be a problem replacing characters in a variable.
Tom L
See eried's post prior to yours; you need to save the result since the function does not modify the input in place.
Ah! I didn't realize that even parameters in predefined functions are passed by value. Thanks!
Tom L
Tom L
Cui bono?
Posts: 2,293
Threads: 52
Joined: Dec 2013
03-31-2017, 03:59 PM
(This post was last modified: 03-31-2017, 04:04 PM by Tim Wessman.)
(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.
TW
Although I work for HP, the views and opinions I post here are my own.
Posts: 2,397
Threads: 61
Joined: Dec 2013
03-31-2017, 05:25 PM
(This post was last modified: 03-31-2017, 05:26 PM by Dieter.)
(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";
x:=replace(x, "apple", "orange");
print(x);
...actually works on the Prime?
And what about your original example and x:=replace(x, " ", "0") then?
Dieter,
just curious. ;-)
Posts: 2,212
Threads: 198
Joined: Dec 2013
(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.
Does this mean that this...
Code: x:="apple pie";
x:=replace(x, "apple", "orange");
print(x);
...actually works on the Prime?
And what about your original example and x:=replace(x, " ", "0") then?
Dieter,
just curious. ;-)
Yes! I can now have my orange pie. and I get 00009 with that replace.
Tom L
Tom L
Cui bono?
|