HP Forums
Best Way to Strip Traling Letter? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Best Way to Strip Traling Letter? (/thread-7987.html)



Best Way to Strip Traling Letter? - toml_12953 - 03-20-2017 05:52 PM

In a program I'm writing, I want to strip the trailing "S" from a word. Since I'm a BASIC programmer, I came up with the following code. It works but I'd like to know if there's a better way to do it on the Prime - a more "Prime-fluent" way, if you will.

Code:
T:=U;
IF RIGHT(T,1)=="S" THEN
  T:=LEFT(T,DIM(T)-1);
END;

Thanks for any advice!

Tom L


RE: Best Way to Strip Traling Letter? - Didier Lachieze - 03-20-2017 08:54 PM

Another way to do it :
Code:
T:=U(1,DIM(U)-(RIGHT(U,1)=="S"))



RE: Best Way to Strip Traling Letter? - Carlos295pz - 03-20-2017 09:49 PM

For "S" and "s" it may is this:
Code:
T:=U(1,DIM(U)-(INSTRING("Ss",RIGHT(U,1))>0))



RE: Best Way to Strip Traling Letter? - Didier Lachieze - 03-20-2017 10:05 PM

(03-20-2017 09:49 PM)Carlos295pz Wrote:  For "S" and "s" it may is this:
Code:
T:=U(1,DIM(U)-(INSTRING("Ss",RIGHT(U,1))>0))

Or:
Code:
T:=U(1,DIM(U)-(UPPER(RIGHT(U,1))=="S"))



RE: Best Way to Strip Traling Letter? - toshk - 03-20-2017 11:05 PM

As in example
Code:

suppress("depends",DIM("depends"))



RE: Best Way to Strip Traling Letter? - Carlos295pz - 03-21-2017 01:33 AM

(03-20-2017 11:05 PM)toshk Wrote:  As in example
Code:

suppress("depends",DIM("depends"))

Under speed test, this form takes approximately 15 times more time of execution than the initial one. It is an alternative, but not massively adequate.

Code:
T:=LEFT(T,DIM(T)-1)

The RIGHT LEFT INSTRING and similar commands are incredibly fast


RE: Best Way to Strip Traling Letter? - toml_12953 - 03-21-2017 03:07 AM

(03-20-2017 08:54 PM)Didier Lachieze Wrote:  Another way to do it :
Code:
T:=U(1,DIM(U)-(RIGHT(U,1)=="S"))

I like it! It's elegant and slightly obscure depending on the fact that a test for equality returns 1 if true. I wasn't aware you could extract substrings using an indexing scheme like that without using string functions (it's the same method used in ISO Full BASIC). This will simplify my programs considerably. Thanks!

Tom L