PDA

View Full Version : string function & loop question



largo_winch
25-09-2012, 10:42
hello, here two new questions from my side:

a) "string$ function"



FUNCTION TBMAIN () AS LONG
LOCAL msg AS STRING
LOCAL l AS INTEGER
msg = "thinBasic is a BASIC-Compiler."
l = LEN(msg)
MSGBOX msg

MSGBOX 0, STRING$(l, "-")
'how to set an underline for a string ?
' for example :

' "thinbasic is a compiler"
' ------------------------- '

END FUNCTION





b) loop question (similar to a problem I try to convert from freebasic to thinbasic): how I can use this command "continue" to follow the loop for three search routines? Can anybody check if my idea with "continue = 1" is a good way?




FUNCTION TBMAIN () AS LONG



END FUNCTION

'---------------------------------------------
FUNCTION filename(BYREF s AS STRING) AS STRING
'=============================================
DIM a AS LONG,b AS LONG, continue AS LONG
DO
a=b+1
'-- b=INSTR(a,s,":") : IF b THEN continue do ' original
b=INSTR(a,s,":") : IF b THEN continue =1'do
b=INSTR(a,s,"\") : IF b THEN continue =1'do
b=INSTR(a,s,"/") : IF b THEN continue =1'do
EXIT DO
LOOP
FUNCTION=MID$(s,a)
END FUNCTION

bye, largo

jack
25-09-2012, 15:42
'-- b=INSTR(a,s,":") : IF b THEN continue do ' original
b=INSTR(a,s,":") : IF b THEN continue =1'do
b=INSTR(a,s,"\") : IF b THEN continue =1'do
b=INSTR(a,s,"/") : IF b THEN continue =1'do


how about

continue=InStr(a,s, Any ":\/")>0

Petr Schreiber
25-09-2012, 16:35
Ad A)
Largo, you almost had it correct. Repeat$ function is what you need to build repeated string.


Function TBMain () As Long

String msg = "thinBasic is a BASIC-like Interpreter."
Long l = Len(msg)

MsgBox 0, msg + $CRLF + Repeat$(l, "-")

End Function



Ad 2)
Option 1
The suggestion Jack just gave is very, very good. When translating code, the best is to use the resources of target language, not just convert line by line.

Option 2
If you would like robot-like translation, in thinBASIC it would look like:


Function TBMain () As Long

MsgBox 0, filename("C:\xFiles\Ufo.txt") ' -- Displays Ufo.txt

End Function

'---------------------------------------------
Function filename(s As String) As String
'=============================================
Long a, b
Do
a = b + 1

b = InStr(a, s, ":") : If b Then Iterate Do
b = InStr(a, s, "\") : If b Then Iterate Do
b = InStr(a, s, "/") : If b Then Iterate Do

Exit Do
Loop

Function = Mid$(s,a)

End Function


Instead of C-like continue keyword, thinBASIC uses the iterate keyword.

I personally dislike the name of continue keyword a lot. Why? Because when I read code, it looks like the code should continue on next line, but it does something different. The iterate keyword is a bit more specific in my opinion.

Option 3
Use the thinBASIC functions! Why write own function to extract file name, when you have it ready for use :)


Uses "file"

Function TBMain () As Long

MsgBox 0, FILE_PathSplit("C:\xFiles\Ufo.txt", %PATH_FILEEXT) ' -- Displays Ufo.txt

End Function



Petr

Michael Clease
26-09-2012, 11:09
this will work



Alias Iterate As Continue

largo_winch
26-09-2012, 17:20
thank you all and for fast replies :) iterate is working here. thanks michael, jack for alternative ways. good to see that's more to learn for me. next questions will come about convertion of "cast" function (function=cast(long,a)) with peek or strptr/varptr problems here with freebasic to thinbasic. I will show my example next time. you're good guys here! bye,largo