Page 1 of 1

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

Posted: Mon May 16, 2022 3:56 pm
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

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

Posted: Mon May 16, 2022 4:41 pm
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


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

Posted: Mon May 16, 2022 4:42 pm
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

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

Posted: Mon May 16, 2022 5:21 pm
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


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

Posted: Mon May 16, 2022 7:07 pm
by skywalk
Yes, for many years, I use a Macro approach similar to what Danilo suggests.
It also works with the C backend compiler.

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

Posted: Mon May 16, 2022 10:38 pm
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.

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

Posted: Tue May 17, 2022 12:05 am
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.