Here is another little library of string functions. These are Basic versions of some useful functions built into the REXX language. The library is V3 dependent as written
I've included my own version of PARSE() which was done before V3 came out with SPLIT. They seem to produce identical results and SPLIT is probably faster, but there it is anyway. In the WORDS function I actually include the two lines necessary to use SPLIT instead of PARSE (commented out).
Some of these are trivial...WORDINDEX works exactly as the new INSTR built-in, but uses the (needle,haystack) parameter order. I have always found that more natural than the (haystack,needle) order...but that's just me.
I've included use of the OPTION_BASE() function to determine the current setting, but, as far as I can tell, Henko's SUBSTR$("10",1,1) technique works great and just as quick. ... but as a matter of principle, I like a language to be able to query any system setting that I can set.
Bill
PS...I edited this post, replacing the original stringrx.txt with a new one with error corrections and more comments
More String Functions
-
- Posts: 21
- Joined: Thu Nov 14, 2013 4:12 pm
More String Functions
- Attachments
-
- stringrx.txt
- Replaced original stringrx with this ... corrections made
- (7.78 KiB) Downloaded 333 times
Last edited by wdmcdaniel on Thu Dec 05, 2013 5:56 am, edited 1 time in total.
- Dutchman
- Posts: 851
- Joined: Mon May 06, 2013 9:21 am
- My devices: iMac, iPad Air, iPhone
- Location: Netherlands
- Flag:
Re: More String Functions
In your function WORDS you write:
SPLIT s$ to a$,words WITH " "
I tested the following examples on the TRANSLATE function
What is wrong with the following single line?'split s$ to a$,n with " " ' alternative using SPLIT command
'words = n ' to use SPLIT, two lines are required
SPLIT s$ to a$,words WITH " "
I tested the following examples on the TRANSLATE function
but got the following unexpected result:{stringrx}
PRINT Translate$("01101","01","10") 'shows: 10010, same as XOR
PRINT Translate$("abracadabra","ba","ab" ) 'shows: 'barbcbdbarb'
PRINT Translate$("abracadabra","","ab" ) 'shows: ' r c d r ',
PRINT Translate$("abracadabra","a","+" ) 'shows: '+br+c+d+br+'
I suppose that something should be cleared before finishing the function.01101
01101abracadabra
01101abracadabraabracadabra
01101abracadabraabracadabraabracadabra
- Dutchman
- Posts: 851
- Joined: Mon May 06, 2013 9:21 am
- My devices: iMac, iPad Air, iPhone
- Location: Netherlands
- Flag:
Re: More String Functions
I modified the TRANSLATE function to get the desired results:
-added the following line at the beginning: 'out$="" ' clear previous usage
and added some code to handle empty 'tblout$': ' IF tblout$="" THEN ………'
10010
barbcbdbarb
rcdr
..r.c.d..r.
+br+c+d+br+
Thanks, a very useful function
-added the following line at the beginning: 'out$="" ' clear previous usage
and added some code to handle empty 'tblout$': ' IF tblout$="" THEN ………'
The result of the examples is:' ================================
' TRANSLATE transforms one string into another
'Examples:
'PRINT Translate$("01101","01","10") 'shows: 10010, same as XOR
'PRINT Translate$("abracadabra","ba","ab" ) 'shows: 'barbcbdbarb'
'PRINT Translate$("abracadabra","","ab" ) 'shows: 'rcdr'
'PRINT Translate$("abracadabra","..","ab" ) 'shows: '..r.c.d..r.'
'PRINT Translate$("abracadabra","+","a" ) 'shows: '+br+c+d+br+'
def TRANSLATE$(s$,tblout$,tblin$)
ob = option_base()
option base 1
out$="" ' clear previous usage
for i = 1 to len(s$) ' scan each char of s$
c$ = mid$(s$,i,1)
p = instr(tblin$,c$,1)
if p > 0 then ' change character
IF tblout$="" THEN
c$=""
ELSE
c$ = mid$(tblout$,p,1)
ENDIF
end if
out$ = out$ & c$
next i
translate$ = out$
if ob = 0 then option base 0
end def
10010
barbcbdbarb
rcdr
..r.c.d..r.
+br+c+d+br+
Thanks, a very useful function
-
- Posts: 21
- Joined: Thu Nov 14, 2013 4:12 pm
Re: More String Functions
Major handslap for me!
You are right...i assumed you could not reference the function name (variable) on the right side of an equation...but forgot that SPLIT is not an equation., so it's fine as you wrote it.
On the clearing out, you are correct again. Not sure how I missed that in testing, though, but I did.
Thanks for the fix .... I'll add it to mine and test more.
Bill
You are right...i assumed you could not reference the function name (variable) on the right side of an equation...but forgot that SPLIT is not an equation., so it's fine as you wrote it.
On the clearing out, you are correct again. Not sure how I missed that in testing, though, but I did.
Thanks for the fix .... I'll add it to mine and test more.
Bill
- Mr. Kibernetik
- Site Admin
- Posts: 4786
- Joined: Mon Nov 19, 2012 10:16 pm
- My devices: iPhone, iPad, MacBook
- Location: Russia
- Flag:
Re: More String Functions
Actually SPLIT s$ to a$,words WITH " " will not work.
Because words will be treated as function call, not as a receiving variable.
But what will work is SPLIT s$ to a$,.words WITH " "
Because words will be treated as function call, not as a receiving variable.
But what will work is SPLIT s$ to a$,.words WITH " "
-
- Posts: 21
- Joined: Thu Nov 14, 2013 4:12 pm
Re: More String Functions
Thanks for the clarification ... had not seen the dot syntax for referencing a variable before
- Mr. Kibernetik
- Site Admin
- Posts: 4786
- Joined: Mon Nov 19, 2012 10:16 pm
- My devices: iPhone, iPad, MacBook
- Location: Russia
- Flag:
Re: More String Functions
Being able to call functions with and without brackets, like
makes it hard to distinguish function call from referencing variable with function name inside the command, like here:
SPLIT s$ to a$,words WITH " "
But if to know that in smart BASIC any function is effectively an executable variable with global scope (global variable which can be "executed" and get some value as a result), then one can write it like this:
SPLIT s$ to a$,.words WITH " "
Here .words is an addressing to function variable.
Code: Select all
def f
enddef
print f;f()
SPLIT s$ to a$,words WITH " "
But if to know that in smart BASIC any function is effectively an executable variable with global scope (global variable which can be "executed" and get some value as a result), then one can write it like this:
SPLIT s$ to a$,.words WITH " "
Here .words is an addressing to function variable.