REPLACE Doesn't.
03-31-2017, 01:40 PM (This post was last modified: 03-31-2017 01:41 PM by toml_12953.)
Post: #1
 toml_12953 Senior Member Posts: 1,938 Joined: Dec 2013
REPLACE Doesn't.
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?
03-31-2017, 01:48 PM (This post was last modified: 03-31-2017 05:28 PM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: REPLACE Doesn't.
(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.
03-31-2017, 01:53 PM
Post: #3
 eried Senior Member Posts: 744 Joined: Dec 2013
RE: REPLACE Doesn't.
You have to store the new result:

replace(x,i,"0");

change to:

x:=replace(x,i,"0");

My website: erwin.ried.cl
03-31-2017, 02:06 PM (This post was last modified: 03-31-2017 02:07 PM by toml_12953.)
Post: #4
 toml_12953 Senior Member Posts: 1,938 Joined: Dec 2013
RE: REPLACE Doesn't.
(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?
03-31-2017, 02:10 PM
Post: #5
 Han Senior Member Posts: 1,882 Joined: Dec 2013
RE: REPLACE Doesn't.
(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.

Graph 3D | QPI | SolveSys
03-31-2017, 02:15 PM
Post: #6
 toml_12953 Senior Member Posts: 1,938 Joined: Dec 2013
RE: REPLACE Doesn't.
(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?
03-31-2017, 03:59 PM (This post was last modified: 03-31-2017 04:04 PM by Tim Wessman.)
Post: #7
 Tim Wessman Senior Member Posts: 2,282 Joined: Dec 2013
RE: REPLACE Doesn't.
(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.
03-31-2017, 05:25 PM (This post was last modified: 03-31-2017 05:26 PM by Dieter.)
Post: #8
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: REPLACE Doesn't.
(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. ;-)
04-01-2017, 12:25 AM
Post: #9
 toml_12953 Senior Member Posts: 1,938 Joined: Dec 2013
RE: REPLACE Doesn't.
(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?
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)