Post Reply 
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
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?
Find all posts by this user
Quote this message in a reply
03-31-2017, 01:48 PM (This post was last modified: 03-31-2017 05:28 PM by Dieter.)
Post: #2
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.
Find all posts by this user
Quote this message in a reply
03-31-2017, 01:53 PM
Post: #3
RE: REPLACE Doesn't.
You have to store the new result:

replace(x,i,"0");

change to:

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

My website: ried.cl
Visit this user's website Find all posts by this user
Quote this message in a reply
03-31-2017, 02:06 PM (This post was last modified: 03-31-2017 02:07 PM by toml_12953.)
Post: #4
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! Smile It seems to be a problem replacing characters in a variable.


Tom L

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
03-31-2017, 02:10 PM
Post: #5
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! Smile 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
Find all posts by this user
Quote this message in a reply
03-31-2017, 02:15 PM
Post: #6
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! Smile 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?
Find all posts by this user
Quote this message in a reply
03-31-2017, 03:59 PM (This post was last modified: 03-31-2017 04:04 PM by Tim Wessman.)
Post: #7
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.
Find all posts by this user
Quote this message in a reply
03-31-2017, 05:25 PM (This post was last modified: 03-31-2017 05:26 PM by Dieter.)
Post: #8
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. ;-)
Find all posts by this user
Quote this message in a reply
04-01-2017, 12:25 AM
Post: #9
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?
Find all posts by this user
Quote this message in a reply
Post Reply 




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