More String Functions

Post Reply
wdmcdaniel
Posts: 21
Joined: Thu Nov 14, 2013 4:12 pm

More String Functions

Post by wdmcdaniel »

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
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.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: More String Functions

Post by Dutchman »

In your function WORDS you write:
'split s$ to a$,n with " " ' alternative using SPLIT command
'words = n ' to use SPLIT, two lines are required
What is wrong with the following single line?
SPLIT s$ to a$,words WITH " "

I tested the following examples on the TRANSLATE function
{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+'
but got the following unexpected result:
01101
01101abracadabra
01101abracadabraabracadabra
01101abracadabraabracadabraabracadabra
I suppose that something should be cleared before finishing the function.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: More String Functions

Post by Dutchman »

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 ………'
' ================================
' 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
The result of the examples is:
10010
barbcbdbarb
rcdr
..r.c.d..r.
+br+c+d+br+

Thanks, a very useful function :)

wdmcdaniel
Posts: 21
Joined: Thu Nov 14, 2013 4:12 pm

Re: More String Functions

Post by wdmcdaniel »

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

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: More String Functions

Post by Mr. Kibernetik »

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 " "

wdmcdaniel
Posts: 21
Joined: Thu Nov 14, 2013 4:12 pm

Re: More String Functions

Post by wdmcdaniel »

Thanks for the clarification ... had not seen the dot syntax for referencing a variable before

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: More String Functions

Post by Mr. Kibernetik »

Being able to call functions with and without brackets, like

Code: Select all

def f
enddef
print f;f()
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.

Post Reply