Page 1 of 1

More String Functions

Posted: Tue Dec 03, 2013 10:55 pm
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

Re: More String Functions

Posted: Wed Dec 04, 2013 8:49 am
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.

Re: More String Functions

Posted: Wed Dec 04, 2013 10:49 am
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 :)

Re: More String Functions

Posted: Wed Dec 04, 2013 10:10 pm
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

Re: More String Functions

Posted: Thu Dec 05, 2013 5:33 am
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 " "

Re: More String Functions

Posted: Thu Dec 05, 2013 6:01 am
by wdmcdaniel
Thanks for the clarification ... had not seen the dot syntax for referencing a variable before

Re: More String Functions

Posted: Thu Dec 05, 2013 6:25 am
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.