Page 1 of 1

Sample string functions

Posted: Wed Jan 02, 2013 9:15 pm
by Dalede
I think this is the place to post some sample programs. Here is one that I wrote as an exercise to understand the function feature and to see if it supported string functions which it does. Basic displays all large numbers in exponential format so I wrote this to reformat them as a long string. Cut and paste this to see how it works. Enjoy!

Dale

rem string function to format a large number
def fl$(a)
b$=a
b=len(b$)-1
for i=0 to b
if substr$(b$,i,i)="E" then goto first:
next i
fl$=b$
return
first:
f$=substr$(b$,0,i-1)
if len(f$)=1 then goto sec:
f$=substr$(b$,0,0) & substr$(b$,2,i-1)
sec:
g=len(f$)
e=substr$(b$,i+1,b)
fl$=f$ & substr$("000000000000",0,e-g)
end def

start:
input a
f$=fl$(a)
print f$, a
goto start:

Re: Sample string functions

Posted: Thu Jan 03, 2013 7:32 pm
by Dalede
Here is a modified version of the above function. This one will handle negative numbers and also really small numbers in addition to the large numbers. Note that you can enter numbers like 1.6E7 in an input statement if you wish to see what it would look like. However you cannot enter 1.6E-7. The input routine evidently can do math on the input numbers and will ignore the E and perform a subtraction. 1.6 + 7 will also be added as will 1.6E+7. Ignoring the E seems like a bug but I like the fact that you can do simple math on an input statement.

rem string function to long format any number
def fl$(a)
b$=a
b=len(b$)-1
for i=0 to b
if substr$(b$,i,i)="E" then goto first
next i
fl$=b$
return
first:
f$=substr$(b$,0,i-1)
if substr$(f$,0,0)="-" then s=1 else s=0
if len(f$)=1+s then goto sec
f$=substr$(b$,0,s) & substr$(b$, 2+s, i-1)
sec:
g=len(f$)-s
e=substr$(b$,i+1,b)
if e>0 then
fl$=f$ & substr$("000000000000",0,e-g)
else
e$="0."
if s then
f$=substr$(f$,1,g)
e$="-0."
endif
fl$=e$ & substr$("000000000000",0,-e-2) & f$
endif
end def

start:
input a
f$=fl$(a)
print f$, a
goto start:

Re: Sample string functions

Posted: Wed Jan 16, 2013 2:59 am
by Dalede
Some other basics have string functions left$, right$, and mid$. This can Be a problem if you want to copy or use one of these programs. The def functions capability in smart basic can be used to emulate these functions. The example shown below provides these functions.

NOTE: when using any function in smart basic be sure it is already defined.

def left$(a$,a)=substr$(a$,0,a-1)
def right$(a$,a)
b=len(a$)
right$=substr$(a$,b-a,b-1)
end def
def mid$(a$,a,b)=substr$(a$,a-1,a+b-2)

Start:
s$="abcdefgh"
print left$(s$,2),right$(s$,3),mid$(s$,3,4)
end

Re: Sample string functions

Posted: Wed Jan 16, 2013 4:02 am
by Mr. Kibernetik
Dalede wrote:Some other basics have string functions left$, right$, and mid$. This can Be a problem if you want to copy or use one of these programs.
Looking at your code I see that left$, right$ and mid$ functions use 1 as a starting index value.
If implementing them as they are it will be out of main indexing concept - start indices from 0 (or from 1 if OPTION BASE 1 command is used).
Do you have any ideas how to workaround this issue if implementing them in smart BASIC?
I don't think that it is a good idea to use 0 as start index in substr$ and use 1 as start index in mid$.

Re: Sample string functions

Posted: Wed Jan 16, 2013 6:10 am
by Dalede
Those functions are simply counting positions in a string. The last position is the same number as the length of the string. In English we count from one and reserve 0 for an empty string. However I struggled a bit with the fact that changing base to 1 would effect string functions as well. When the base is changed all of those functions would have to be adjusted. At least adjusting the functions themselves would automatically fix all the places they are used. I am not used to a basic when the strings are treated as arrays and are indexed from 0.

Dale

Re: Sample string functions

Posted: Sun Jan 20, 2013 4:55 am
by Elchoud
Dalede, Thank You for Posting this, I was looking for it

Re: Sample string functions

Posted: Tue Mar 26, 2013 5:15 pm
by Dalede
Mr. Kibernetik wrote:
Dalede wrote:Some other basics have string functions left$, right$, and mid$. This can Be a problem if you want to copy or use one of these programs.
Looking at your code I see that left$, right$ and mid$ functions use 1 as a starting index value.
If implementing them as they are it will be out of main indexing concept - start indices from 0 (or from 1 if OPTION BASE 1 command is used).
Do you have any ideas how to workaround this issue if implementing them in smart BASIC?
I don't think that it is a good idea to use 0 as start index in substr$ and use 1 as start index in mid$.
I have thought about this some more and I remembered how Microsoft did strings. (I disassembled y Commodore PET firmware.) If you think of each character as an array then the 0th element contains the length of the array. If you think of characters as strings then the length of the string is stored in the first position. This is unlike C which marks the end of the string with a 0. In the early days of Basic only 8 bit ASCII was supported so each character was one byte and the first element was also 1 byte limiting the length of strings to 255 characters. In practice this was not a serious limitation.

Dale

Re: Sample string functions

Posted: Tue Mar 26, 2013 6:24 pm
by Mr. Kibernetik
Yes, this is a good topic to think about when expanding string functions set.