Post Reply 
HP49-50G: transform "ABCDEF" into "ABDEF" (with no SUB?)
12-30-2020, 05:11 PM
Post: #1
HP49-50G: transform "ABCDEF" into "ABDEF" (with no SUB?)
I have a string "ABCDEF".

I look, say, for the letters "CD".

If that strings "CD" exists,
then I want to change it by "D" (with no "C").

The initial string "ABCDEF" should
become "ABDEF" (without the letter C).

My first idea was to write
<<
"ABCDEF"
DUP
"CD"
POS
"" // Empty string (instead of "C")
REPL // to be put instead of the original letter "C"
>>

But it does not work
("" REPL meaning "do not replace [the letter C]").

One "heavy" solution is then:

\<< "ABCDEF" DUP SIZE \-> str siz
\<< str "CD" POS \-> pos
\<< str 1 pos 1 - SUB
str pos 1 + siz SUB
+
\>>
\>>
\>>

Question:
How to write a shorter version,
possibly with the instruction REPL
(and not by concatenating
...Sub... ...SUB +)?

Sorry for that simple question,
but I would appreciate your insights.

Regards,
Gil
Find all posts by this user
Quote this message in a reply
12-30-2020, 06:41 PM
Post: #2
RE: HP49-50G: transform "ABCDEF" into "ABDEF" (with no SUB?)
The command you are looking for is SREPL. It is new to the 49g+/50g. SREPL returns the modified string to level 2 and a count of the number of replacements made to level 1.

"ABCDEF"
"CD"
"D"
SREPL
-> "ABDEF"
1.
Find all posts by this user
Quote this message in a reply
12-30-2020, 06:49 PM
Post: #3
RE: HP49-50G: transform "ABCDEF" into "ABDEF" (with no SUB?)
Exactly what I was looking for!

Great that that command returns also 1 when the string is found and replaced,
and 0 when the request could not be fulfilled.

Thank you and a happy new year to the whole community!

Regards,
Gil
Find all posts by this user
Quote this message in a reply
12-31-2020, 01:22 PM
Post: #4
RE: HP49-50G: transform "ABCDEF" into "ABDEF" (with no SUB?)
(12-30-2020 06:49 PM)Gil Wrote:  Exactly what I was looking for!

Great that that command returns also 1 when the string is found and replaced,
and 0 when the request could not be fulfilled.

Or something greater than 1 if multiple matching sub-strings were replaced. Is that what you want?

— Ian Abbott
Find all posts by this user
Quote this message in a reply
12-31-2020, 07:37 PM
Post: #5
RE: HP49-50G: transform "ABCDEF" into "ABDEF" (with no SUB?)
Yes, and thanks for your correction.

I already integrated that new command in my fraction program posted this week.

The number of occurrences and replacements was not relevant, so I dropped that information.

The purpose of SREPL was
to change expressions of the type '-7+-3/4` into '-7-3/4'.

Regards,
Gil
Find all posts by this user
Quote this message in a reply
01-01-2021, 10:28 AM
Post: #6
RE: HP49-50G: transform "ABCDEF" into "ABDEF" (with no SUB?)
As an alternative, you could tokenize your expression, replace the wanted sub-expression, and finally rebuild the expression again.
This is how the HP 48 does it internally.

-- Ray
Find all posts by this user
Quote this message in a reply
01-01-2021, 10:55 AM
Post: #7
RE: HP49-50G: transform "ABCDEF" into "ABDEF" (with no SUB?)
It's somewhat what I did at the beginning.

The ideal for me is, as mentioned, to use the SREPL.

Thanks and regards,
Gil
Find all posts by this user
Quote this message in a reply
Post Reply 




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