Results 1 to 9 of 9

Thread: scalar string variable

  1. #1

    Question scalar string variable

    Hi,
    I've seen some time this expression in help pages : "scalar string variable" (LENF, LEFTF$, PARSEF$ ...)

    I didn't understand it well. Is it something like "12345" ?

    • Can you tell what is the right question to self make the difference between scalar and non-scalar?
    • With example of each please ?



    Thanks
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,146
    Rep Power
    735
    Hi Sebastian,

    the opposite of scalar string variable is string array.

    Scalar variable is variable able to hold one value at the time, so:
    STRING s = "ciao"
    ' or
    DIM s AS STRING = "ciao"
    
    Array variable can hold multiple variables:
    STRING s(3) = "ciao", "hola", "hellou"
    ' or
    DIM s(3) AS STRING = "a", "b", "c"
    
    The key for the functions you mention is that you must specify variable, not string literal:
    string input = "hola hola"
    
    string result = leftf$(input, 4) ' Correct, variable input passed as param
    
    string result = leftf$("hola hola", 4) ' Wrong, string literal "hola hola" passed as param directly, instead of via variable name
    

    Petr
    Last edited by Petr Schreiber; 06-01-2020 at 22:36.
    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

  3. #3

    Thumbs up

    Thanks Petr !
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,814
    Rep Power
    10
    Pay attention: there are some FAST version of some standard functions. For example:
    LEN and LENF
    LEFT$ and LEFTF$
    MID$ and MIDF$
    RIGHT$ and RIGHTF$
    GRAB$ and GRABF$

    The difference is that the STANDARD version of the function accepts any string expression while the FAST version of the function accepts only scalar dynamic string variables.
    Why is FAST? Because instead of parsing a string expression and allocate memory for it, it just use a pointer to the scalar string variable and it is super super fast and no memory allocation takes place.

    For example if you pass 100MB string to MID$ it created a new copy of your 100MB string, perform the MID$ function, de-allocate the copy of the 100MB string and return
    Very inefficient
    While MIDF$ just perform the MID using the memory area of the scalar string variable passed.
    It is like the difference of passing a parameter BYCOPY or BYREF


    An example?
    Check by yourself how much faster it is
    uses "console"
    
    Double T1, T2
    
    
    '---Allocate a 10MB string
    string S(2) 
    s(1) = repeat$(1000000, "ABCDEFGHIJ")
    string X
    Long L
    long N
    
    
    Print "Time to perform 100  times MID$ + LEN on a 10MB string:", "..."
    T1 = Timer
    for n = 1 to 100
      x = Mid$(s(1), 1000, 10)
      x = Left$(s(1), 10)
      x = Right$(s(1), 10)
      L = len(s(1))
    Next
    T2 = Timer
    PrintL format$(T2-T1, "#0.000000")
    
    
    Print "Time to perform 100K times fast mid MIDF$ + LENF on a 10MB string:", "..."
    T1 = Timer
    for n = 1 to 100000
      x = MidF$(s(1), 1000, 10)
      x = Leftf$(s(1), 10)
      x = Rightf$(s(1), 10)
      L = lenF(s(1))
    Next
    T2 = Timer
    PrintL format$(T2-T1, "#0.000000")
    
    
    PrintL
    printl "---Press a key to continue---"
    WaitKey
    
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5

    Question

    Thanks for this.

    One last thought. Tell me if I'm wrong.

    LEN, LEFT$, MID$ ... are mostly of no more use because if it serve for string literal ("strings like this, right ?") , the operation should be computed once by the programmer at scripting time (before script execution) ?
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,814
    Rep Power
    10
    No, no. Sorry maybe I confused

    LEN, LEFT$, MID$, .... and in general all string functions work with what ever string or what ever string expression.

    The FAST versions of some of the string functions instead (when avaibale) only work with single variable or single string element of a dynamic array.


    Last edited by ErosOlmi; 08-01-2020 at 11:02.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  7. #7

    Question

    Quote Originally Posted by ErosOlmi View Post
    The FAST versions of some of the string functions instead (when avaibale) only work with single variable or single string element of a dynamic array.
    Then I understand that regular versions must work with something else (more).

    Quote Originally Posted by ErosOlmi View Post
    LEN, LEFT$, MID$, .... and in general all string functions work with what ever string or what ever string expression.
    There, this is what confuses me. What could that be else than single variable or single string element ? Even the previous examples applies the keywords to single variable or single string element of a dynamic array. Only this
    string result = leftf$("hola hola", 4) 'wrong
    'should be
    string result = left$("hola hola", 4)
    'or better
    string result = "hola" ' because it can be established before runtime
    
    is different, but then match with
    Quote Originally Posted by DirectuX View Post
    LEN, LEFT$, MID$ ... are mostly of no more use because if it serve for string literal ("strings like this, right ?") , the operation should be computed once by the programmer at scripting time (before script execution) ?
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  8. #8
    for information only , the beginning history of MidF$ is 2 years ago, look Eros post here http://www.thinbasic.com/community/s...3999#post93999
    i think Eros can't remove Mid$ and replace it by Midf$ because Mid$ is in standard basic and it is by nature slow for big tasks, but acceptable for everyday usual tasks
    while Midf$ is like a superman when Clara say superman superman!! and then he come to the rescue, it is fast because it is using the pointers. as it is said Midf$ operate over string variables and not over explicit string like "hello world" it is then will give an error msg 'Expected a variable but found something else. Token is not a variable'
    Uses "Console"
    
    String a,b
    a="hello world"
    b=Mid$(a, 4,5)
    PrintL b
    b=Midf$(a, 4,5)
    PrintL b
    b=Mid$("hello world", 4,5) 
    PrintL b
    b=Midf$("hello world", 4,5) 
    PrintL b
    '---Wait for a key press
    WaitKey
    

  9. #9
    This is not about speed or getting errors,

    I want (would like) to understand this.
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

Similar Threads

  1. help please, variable definition
    By DirectuX in forum OOP
    Replies: 4
    Last Post: 10-02-2024, 08:24
  2. String-in-String-Pointers?
    By ReneMiner in forum thinBasic General
    Replies: 9
    Last Post: 11-06-2013, 14:55

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

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