Best Way to Strip Traling Letter?
03-20-2017, 05:52 PM
Post: #1
 toml_12953 Senior Member Posts: 544 Joined: Dec 2013
Best Way to Strip Traling Letter?
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;

Tom L
03-20-2017, 08:54 PM (This post was last modified: 03-20-2017 09:09 PM by Didier Lachieze.)
Post: #2
 Didier Lachieze Senior Member Posts: 793 Joined: Dec 2013
RE: Best Way to Strip Traling Letter?
Another way to do it :
Code:
T:=U(1,DIM(U)-(RIGHT(U,1)=="S"))
03-20-2017, 09:49 PM (This post was last modified: 03-20-2017 09:50 PM by Carlos295pz.)
Post: #3
 Carlos295pz Member Posts: 112 Joined: Sep 2015
RE: Best Way to Strip Traling Letter?
For "S" and "s" it may is this:
Code:
T:=U(1,DIM(U)-(INSTRING("Ss",RIGHT(U,1))>0))

Viga C | TD | FB
03-20-2017, 10:05 PM
Post: #4
 Didier Lachieze Senior Member Posts: 793 Joined: Dec 2013
RE: Best Way to Strip Traling Letter?
(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"))
03-20-2017, 11:05 PM
Post: #5
 toshk Member Posts: 127 Joined: Feb 2015
RE: Best Way to Strip Traling Letter?
As in example
Code:
 suppress("depends",DIM("depends"))
03-21-2017, 01:33 AM
Post: #6
 Carlos295pz Member Posts: 112 Joined: Sep 2015
RE: Best Way to Strip Traling Letter?
(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

Viga C | TD | FB
03-21-2017, 03:07 AM (This post was last modified: 03-21-2017 03:07 AM by toml_12953.)
Post: #7
 toml_12953 Senior Member Posts: 544 Joined: Dec 2013
RE: Best Way to Strip Traling Letter?
(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
 « Next Oldest | Next Newest »

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