This is not for advanced users, this is just something I am thinking about, that may be (or may be not) useful to someone.
Well, I was revisiting the file I/O functions that come with DBPro and many things came to my mind.
With FIRST TOKEN$() and NEXT TOKEN$() functions, I can store a string inside a file and then retrieve it and parse it to get different data and store it in different variables, like this:
Supposing that the retrieved string is something like this:
String$ = "$Name$Age$Address"
Name$= FIRST TOKEN$(String$,"$")
age = val(NEXT TOKEN$("$"))
Address$ = NEXT TOKEN("$")
And we took two strings and one integer from a single string. Great, it is kinda useful.
Now, suppose we want to store the same 3 variables back to the file, so we may do:
String$ = "$" + name$ + "$" + str$(age) + "$" + Address$
WRITE STRING file,String$
Right, we can use strings to everything and read or write them easily inside files, and this solves many problems related to how many different data types we can get.
This solved a problem I was facing: UDT arrays can't be saved properly - in fact, they can, but they cannot be loaded back, at least it seems so.
Transforming everything into strings makes UDT arrays saveable this way:
TYPE custom
name as string
age as integer
address as string
ENDTYPE
dim list(0) as custom
array insert at top list(0)
list().name = "John"
list().age = 35
list().address = "Main Street, 112"
String$ = "$"+list().name + "$" + str$(list().age) + "$" + list().address
Then just write the string and it is saved safe and sound.
And to retrieve the UDT array index, the same thing can accomplished with tokens - is this why I added some "$" in the strings.
READ STRING 1,string$
list().name = first token$(string$,"$")
list().age = val(NEXT TOKEN$("$"))
list().address = NEXT TOKEN$("$")
I know it is kinda silly, but I believe many people found issues trying to make stuff like this.
Now, there is something I need to figure out how to make properly:
RGB colors.
I store them as 3 separated fields - 3 numbers that range from 0 to 255 - like this: $255$255$255 - and then I retrieve all of them and store each one into an integer and then pass them to a dword using the RGB() function.
Is there a better way to store these values?
I tried an hex 0xFFFFFF but I could not rebuilt it after reading, because it comes as a string and the VAL() function simply ignores it. Why?
Color$ = "0xFFFFFF"
color1 = val(STRING$)
CLS color1
It should paint the white color, but it paints black, because color1 = 0, not 0xFFFFFF. Finding out how to transform "0xFFFFFF" into rgb(r,g,b) should save me some space and time.
Another - and this is not related to string parsing:
Since my native language is Portuguese, I need accents everywhere. We use letters like "á" or "ô" all the time.
What intrigues me is:
If a string has these characters, when I print it to the screen at sizes bigger than 30, the characters with accents are replaced by rectangles or corrupted characters. Why?
FOR size = 100 to 19 step 5
SET TEXT SIZE size
PRINT "Teste de acentuação " + str$(size)
NEXT size
With this you can see what I am talking about.
I tried setting the codepage to PTBR, but the result is the same. Strangely enough, a small number of fonts don't show this peculiarity and accents are shown properly regardless of the text size.
Well, that's all for now.
Sharing in here all my small findings and thoughts is what I can do to contribute in here. For advanced users, my stuff is useless, but I bet that some other newbies would find them inspiring.
Thank you. End of transmission.
[size=+2]Forever and one[/size]