View Full Version : scalar string variable
DirectuX
06-01-2020, 15:05
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
Petr Schreiber
06-01-2020, 22:33
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
DirectuX
06-01-2020, 22:38
Thanks Petr !
ErosOlmi
07-01-2020, 23:48
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
DirectuX
08-01-2020, 10:41
Thanks for this. :good:
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) ?
ErosOlmi
08-01-2020, 10:53
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.
DirectuX
08-01-2020, 15:39
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).
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
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) ?
for information only , the beginning history of MidF$ is 2 years ago, look Eros post here http://www.thinbasic.com/community/showthread.php?12821-Comparing-Mid-with-string-pointer&p=93999#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
DirectuX
09-01-2020, 16:12
This is not about speed or getting errors,
I want (would like) to understand this (https://www.thinbasic.com/community/showthread.php?13006-scalar-string-variable&p=95293&viewfull=1#post95293).