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