Datasection Data int,string,int,string....

Just starting out? Need help? Post your questions and find answers here.
hoerbie
Enthusiast
Enthusiast
Posts: 119
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Datasection Data int,string,int,string....

Post by hoerbie »

Hi,

in a program I need a datasection where a lot of pairs of an integer value and a string are defined and read in. In the PB docs the examples only show multiple values of the same type separated by comma, so I think of using something like

Code: Select all

DataSection
  mylabel:
  Data.i 1
  Data.s "TextABC"
  Data.i 17
  Data.s "SomeText"
  ...
EndDataSection
would be fine, but it gets really long with more data.

Coming from good old GFA Basic there no type needs to be defined, so there it was possible to do it with

Code: Select all

DATA 1,"TextABC",17,"SomeText"
and then simply doing a loop for reading an int and reading a string.

I've found out, that something like

Code: Select all

DataSection
  mylabel:
  Data.i 1,"TextABC",17,"SomeText"
EndDataSection

Restore mylabel
Read.i num(1)
Read.s txt(1)
Read.i num(2)
Read.s txt(2)
works in PB, so with only defining the number values type, the mixed numbers and strings can be read by PB 5.73.

But as this isn't documented in the (german) help, I would like to be sure, that this is an accepted way, that will not be killed by an update ob PB in the future.
My only other idea would be to put the numbers into the string and use a separating char between number and string, define and read only strings and split them with StringFields.

Or does anyone have a tip to make it better? As in real life the data list is really long, it is not possible to first define all numbers and then define all strings.

I've tried to find other solutions, but the keywords DataSection, Data und Read are used a bit to often.

Greets, hoerbie
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Datasection Data int,string,int,string....

Post by mk-soft »

This not work with pb v5.73 and since pb v6.00 check this as failure ...

Code: Select all

Dim num.i(2)
Dim txt.s(2)

Restore mylabel
Read.i num(1)
Read.s txt(1)
Read.i num(2)
Read.s txt(2)

Debug num(1)
Debug txt(1)
Debug num(2)
Debug txt(2)


DataSection
  mylabel:
  Data.i 1,"TextABC",17,"SomeText"
EndDataSection

Last edited by mk-soft on Mon May 16, 2022 4:43 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Datasection Data int,string,int,string....

Post by Danilo »

If it is always a pair of int and string, I would create a simple Macro:

Code: Select all

Macro DataPair(_one_,_two_)
    Data.i _one_
    Data.s _two_
EndMacro

DataSection
  mylabel:
  DataPair(1,"TextABC") : DataPair(17,"SomeText")
EndDataSection

Restore mylabel
;Read.i num(1)
;Read.s txt(1)
;Read.i num(2)
;Read.s txt(2)

Define s.s, x.i

Read.i x : Debug x
Read.s s : Debug s
Read.i x : Debug x
Read.s s : Debug s
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Datasection Data int,string,int,string....

Post by mk-soft »

Attention! For professionals only.

If used incorrectly, it leads to a programme crash and is not recommended.
So it's best to look at it once and then forget about it right away.

Code: Select all

; !!! Never write string to Array MyInitData (crash) !!!

Structure udtMyData ; Only type Intger / String
  iVal.i
  Str.s
EndStructure

Dim MyInitData.udtMyData(1)
Dim MyWorkData.udtMyData(1)

CopyMemory(?mylabel, @MyInitData(), SizeOf(udtMyData) * 2)


Debug "MyInitData"
For i = 0 To ArraySize(MyInitData())
  Debug "Index " + i + ": iVal = " + MyInitData(i)\iVal + " / pStr = " + MyInitData(i)\Str
Next

; Crashed because is not real pb string
; MyInitData(0)\Str = #Null$
; FreeArray(MyInitData())

CopyArray(MyInitData(), MyWorkData())
MyWorkData(1)\iVal = 100
MyWorkData(1)\Str = "Hello World!"

Debug "MyWorkData"
For i = 0 To ArraySize(MyWorkData())
  Debug "Index " + i + ": iVal = " + MyWorkData(i)\iVal + " / pStr = " + MyWorkData(i)\Str
Next

DataSection
  mylabel:
  Data.i 1,@"TextABC",17,@"SomeText"
EndDataSection

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Datasection Data int,string,int,string....

Post by skywalk »

Yes, for many years, I use a Macro approach similar to what Danilo suggests.
It also works with the C backend compiler.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Jeff8888
User
User
Posts: 38
Joined: Fri Jan 31, 2020 6:48 pm

Re: Datasection Data int,string,int,string....

Post by Jeff8888 »

Could you make the data section pairs of strings such as "1.0","abc",....etc. Then use Val() to convert first string of each pair to a number.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Datasection Data int,string,int,string....

Post by skywalk »

Yes, but then you are building your datasection with all strings.
Not a big deal for small amounts of data.
Don't forget Val("123") for integers and ValD("1.23") for floating points.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply