HP Forums

Full Version: REPLACE Doesn't.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
(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.
You have to store the new result:

replace(x,i,"0");

change to:

x:=replace(x,i,"0");
(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
(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.
(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
(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.
(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. ;-)
(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
Reference URL's