View Full Version : Idea for LEFT$/RIGHT$ variable that work in reverse
Robert Hodge
29-06-2013, 17:06
The string functions LEFT$ and RIGHT$ retain the left or right side of a string for a given length, and discard the rest.
For example, LEFT$ ("ABCD", 1) returns "A" and RIGHT$ ("ABCD", 1) returns "D".
However, suppose the left or right side for a given length is what you DON'T want - you really want to "extract" the left or right side, and return what remains. Let's call this an "extract" function, and let's suppose there are two new function names, and XRIGHT$.
Then, for example, XLEFT$ ("ABCD", 1) returns "BCD" and XRIGHT$ ("ABCD", 1) returns "ABC".
Now, XLEFT$(string,n) is essentially the same as MID$(string,n+1), but there is no easy way to use MID$ for XRIGHT$, even if you use a negative starting point. The only way to do XRIGHT$(string,n) is by saying LEFT$(string, LEN(string)-n)). And, using such constructs is not at all obvious or intuitive, and is tedious enough to be error-prone to type.
I believe that clearer code could be made if XLEFT$ and XRIGHT$ were available.
Charles Pegge
29-06-2013, 18:04
Negative indexes can be used to indicate displacement from the right. They are quite easy to implement in a BASIC interpreter:
Internally: if index<0 then index+=len(string)+1
thus for the mid$ function:
mid ("1234567890",1,3) 'LEFT result: "123"
mid ("1234567890",-2) 'RIGHT result: "90"
mid ("1234567890",3,2) 'MID result: "34"
mid ("1234567890",-3,2) 'MID result: "89"
ReneMiner
29-06-2013, 20:58
Left$/Right$/Mid$ are all very slow functions which create temporary strings - this is why I prefer peeking strings like that:
Uses "console"
Dim A As String = "Hello Kitty!"
PrintL Extract(A, 1, 5)
PrintL Extract(A, 7, 12)
WaitKey
Function Extract(ByVal From As String, ByVal First As Long, ByVal Last As Long) As String
Function = Peek$(StrPtr(From) + First-1, Last + 1 - First)
End Function
but nevermind - I would not call a function as done here (since it creates some temp$ "From") - but write the functions result directly to code
Robert Hodge
29-06-2013, 21:03
Negative indexes can be used to indicate displacement from the right. They are quite easy to implement in a BASIC interpreter:
Internally: if index<0 then index+=len(string)+1
thus for the mid$ function:
mid ("1234567890",1,3) 'LEFT result: "123"
mid ("1234567890",-2) 'RIGHT result: "90"
mid ("1234567890",3,2) 'MID result: "34"
mid ("1234567890",-3,2) 'MID result: "89"
The XLEFT$ (string,n) is easy enough using MID$(string, n+1).
But, the XRIGHT$ (string, n) is quite a bit harder. Let's take your example above. Suppose I have "1234567890" and I want everything BUT the right "0". Using a negative index won't work:
MID$ ("1234567890", -1) = "0" which is the same as RIGHT$ ("1234567890", 1).
Let's say S = "1234567890"
To get the desired result, I have to use LEFT$ (S, LEN(S) -1).
Robert Hodge
29-06-2013, 23:00
I have been busy this week and didn't have a chance to do my normal walkabout through the thinBasic posts. I see Charles beat me to posting the obvious yet I detect there is still a burning desire to add more keywords (preferably starting with the letter X) I think Eros has spoiled his users and needs to use the RTFM and use the BASIC syntax provided icon a bit more.
Not that familiar with X names except XPRINT.
Can XLEFT$ and XRIGHT$ be done with LEFT$, RIGHT$ or MID$. Yes, no question. Just not as easily.
Am I spoiled? Since Eros hasn't done that many things for me, then I'd have to say No, not spoiled (yet). What can I say? All I can do is ask, and all he will do is either say Yes or No. I just have to ask nicely enough that at least he'd *consider* saying Yes.
ReneMiner
30-06-2013, 09:19
...I just have to ask nicely enough that at least he'd *consider* saying Yes.
I don't think it's about how you ask or demand it - assumed it's friendly anyway.
But about if it's doable at first. And if so - can I do it myself with already available methods? - which lowers the priority and demands an example of functionality written in tB (appealing to someones own honor) - just to show how easy the function could be developed or implemented.
Second, does one really need it? - where one is mostly the creator - or is it at least cool feature or enrichement of language. I can not remember that I ever needed an excluded part of a text-string. But I can imagine to have an array of numbers, peek them as string and remove certain elements from it so to put the leftover back into my numeral array...
Third is first again: provide some example of functionality in stunning ease so the ambition spurres the creator...
ErosOlmi
30-06-2013, 10:26
Am I spoiled? Since Eros hasn't done that many things for me, then I'd have to say No, not spoiled (yet). What can I say? All I can do is ask, and all he will do is either say Yes or No. I just have to ask nicely enough that at least he'd *consider* saying Yes.
quatras9,
do not mind to ... (better I keep calm). Just ask and I will be happy to consider.
For those not knowing the situation: Quatras9 with another guy George (also registered in thinBasic community) are using thinBasic Core engine as embedded script engine in they ISPF clone editor http://spflite.co.nr/
They helped me a lot pushing me to check and solve many bugs that were preventing thinBasic to be used as embeddable script engine into 3rd party applications. I'm still fixing some situations not coherent with the embedding concept but the big step has been done.
Now Quatras9 is asking more and different commands because they software had already in the past a script engine using REXX like language that had many functions. So porting old commands is not easy if there are not similar commands into new engine (thinBasic)
Ciao
Eros