Results 1 to 5 of 5

Thread: string function & loop question

  1. #1

    string function & loop question

    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

  2. #2
    Quote Originally Posted by largo_winch View Post
        '-- 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
    

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,083
    Blog Entries
    5
    Rep Power
    725
    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
    Last edited by Petr Schreiber; 25-09-2012 at 17:24.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Blog Entries
    1
    Rep Power
    156

    Lightbulb work around the continuum issue

    this will work

    Alias Iterate As Continue
    
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

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

Similar Threads

  1. ThinBasic Design question - ever running loop - how to stop ?
    By chris_bdnsee in forum thinBasic General
    Replies: 5
    Last Post: 15-03-2009, 20:13
  2. Problem with returning a string from a function
    By DavidMead in forum Module SDK (Power Basic version)
    Replies: 4
    Last Post: 26-11-2008, 12:51
  3. CALL keyword with function name as string expression
    By ErosOlmi in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 0
    Last Post: 07-09-2006, 16:16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •