Counting Items in the DataSection?

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Counting Items in the DataSection?

Post by mk-soft »

Just like you shouldn't do it ...

Code: Select all

;-TOP

Global Dim Fruit.s(0) ; <- Read Only

Procedure InitFruit()
  Protected items, *c.Character, *s , *arr.integer
  *c = ?FruitList
  *s = *c
  Repeat
    If *c\c = #ETX
      Break
    EndIf
    If *c\c = 0
      ReDim Fruit(items)
      *arr = @Fruit() + items * SizeOf(integer)
      *arr\i = *s
      *s = *c + SizeOf(character)
      items + 1
    EndIf
    *c + SizeOf(Character)
  ForEver
  ProcedureReturn items
EndProcedure

Procedure ReleaseFruit()
  Protected *arr.integer = @Fruit()
  Protected items = ArraySize(Fruit())
  Protected index
  For index = 0 To items
    *arr\i = 0
    *arr + SizeOf(integer)
  Next
EndProcedure

i = InitFruit()
Debug "Count: " + i
For index = 0 To ArraySize(Fruit())
  Debug "Index " + index + ": " + Fruit(index)
Next

ReleaseFruit()

DataSection
  FruitList:
  Data.s "Apple", "Orange"
  Data.s "Mango", "Apricot"
  Data.s "Banana", "Pear"
  Data.s "Peach", "Tangerine"
  Data.s #ETX$
EndDataSection
PB can not regular free strings
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
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Counting Items in the DataSection?

Post by Blue »

Why complicate simple things ?
My rewrite of Shardik's simple (and best) solution :


Code: Select all

Procedure CountFruits()
  Count = 0
  Restore FruitList
  Repeat
    Read.S Fruit$
    If #ETX$ = Fruit$ : ProcedureReturn Count : EndIf
    Count + 1
  ForEver
EndProcedure

DataSection
  FruitList:
  Data.s "Apple", "Orange"
  Data.s "Mango", "Mango"
  Data.s #ETX$
EndDataSection

Debug CountFruits()
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Counting Items in the DataSection?

Post by Little John »

Another variation of Shardik's code: :-)

Code: Select all

Procedure.i CountFruits()
   Count = -1
   Restore FruitList
   Repeat
      Read.s Fruit$
      Count + 1
   Until Fruit$ = #ETX$
   ProcedureReturn Count
EndProcedure

DataSection
   FruitList:
   Data.s "Apple", "Orange"
   Data.s "Mango", "Pear"
   Data.s #ETX$
EndDataSection

Debug CountFruits()
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Counting Items in the DataSection?

Post by Blue »

Little John wrote: Mon May 05, 2025 5:38 am Another variation of Shardik's code: :-)

Code: Select all

Procedure.i CountFruits()
   Count = -1
   [. . .]
EndProcedure
   [. . .]
Smart ! :thumbsup:
And that’s as good as it gets.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Counting Items in the DataSection?

Post by Michael Vogel »

You can create tons of variations... :lol:

Code: Select all

Procedure.i CountFruits()

	Protected Count
	Protected *mem=?FruitList
	
	Repeat
		Count+Bool(PeekC(*mem)=0)
		*mem+SizeOf(Character)
	Until *mem=?EndFruitList
	
	ProcedureReturn Count
	
EndProcedure

DataSection
	FruitList:
	Data.s "Apple", "Orange"
	Data.s "Mango", "Pear"
	EndFruitList:
EndDataSection

Debug CountFruits()
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: Counting Items in the DataSection?

Post by AZJIO »

Blue wrote: Sun May 04, 2025 8:55 pm Why complicate simple things ?
The code authors gave good ideas for understanding the data section. One of them is that the data are strings ending in zero and following each other. In this regard, you can process the data in any convenient way, not just by reading lines.
The method of counting zeros is not complicated, perhaps the most ideal. This works the same way as the CountString() function.
The idea with #PB_Compiler_Line is also ideal, as it allows you to insert the correct value WITHOUT CALCULATIONS.
So you shouldn't be underestimating the examples.

Code: Select all

DataSection
	FruitList:
	#FruitList_S = #PB_Compiler_Line
	Data.s "Apple", "Orange"
	Data.s "Mango", "Apricot"
	Data.s "Banana", "Pear"
	Data.s "Peach", "Tangerine"
	#FruitList_E = #PB_Compiler_Line
	FruitListend:
	Data.i (#FruitList_E - #FruitList_S - 1) * 2
EndDataSection

Restore FruitListend
Read.i tmp
Debug tmp
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Counting Items in the DataSection?

Post by mk-soft »

Actually, it makes no sense to return only the number of entries.
You also need the content. So you can and should also transfer the content straight away.

Code: Select all

;-TOP

Global Dim Fruit.s(0)

Procedure InitFruit()
  Protected temp.s, count, size
  
  size = ArraySize(Fruit())
  Restore FruitList
  
  Repeat
    Read.s temp
    If temp = #ETX$
      Break
    EndIf
    If size < count
      size + 10
      ReDim Fruit(size)
    EndIf
    Fruit(count) = temp
    count + 1
  ForEver
  If size >= count
    ReDim Fruit(count - 1)
  EndIf
  ProcedureReturn count
EndProcedure

cnt = InitFruit()
Debug "Count: " + cnt
For index = 0 To ArraySize(Fruit())
  Debug "Index " + index + ": " + Fruit(index)
Next

DataSection
  FruitList:
  Data.s "Apple", "Orange"
  Data.s "Mango", "Apricot"
  Data.s "Banana", "Pear"
  Data.s "Peach", "Tangerine"
  Data.s #ETX$
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
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Counting Items in the DataSection?

Post by Michael Vogel »

Not sure if it does make sense to duplicate the data memory to string memory :)

I'd think there are enough answers for counting the items already - but because Pandora's box was opened...

Code: Select all

Global Dim Fun.s(0)

Procedure.i CountFruits4Fun()

	Protected Count
	Protected *mem=?FruitList

	PokeI(@Fun(),*mem)
	Repeat
		If PeekC(*mem)
			*mem+SizeOf(Character)
		Else
			Count+1
			*mem+SizeOf(Character)
			If *mem=?EndFruitList
				ProcedureReturn Count
			Else
				ReDim Fun(Count)
				PokeI(@fun()+Count*SizeOf(Integer),*mem)
			EndIf
		EndIf
	ForEver

EndProcedure

DataSection
	FruitList:
	Data.s "Apple", "Orange"
	Data.s "Mango", "Pear"
	EndFruitList:
EndDataSection

n=CountFruits4Fun()
For i=0 To n-1
	Debug fun(i)
Next i
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Counting Items in the DataSection?

Post by mk-soft »

Michael Vogel wrote: Wed May 07, 2025 8:16 am Not sure if it does make sense to duplicate the data memory to string memory :)

I'd think there are enough answers for counting the items already - but because Pandora's box was opened...
We already had that (from me). But rules must be observed. Read-only access and release the pointers on the string itself.
Otherwise crash
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
Skipper
User
User
Posts: 40
Joined: Thu Dec 19, 2024 1:26 pm
Location: NW-Europe

Re: Counting Items in the DataSection?

Post by Skipper »

Call me daft, but why would you want to count the number of items in a data section?
We are the programmers in charge, we (should) know how many items we put in. The data section list is a static list in the source code, after compilation this doesn't change during runtime. Why count these items at all? The programmer has this information already...

Confused
Skipper
miso
Enthusiast
Enthusiast
Posts: 409
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Counting Items in the DataSection?

Post by miso »

Confused
Don't be. It's a riddle solving game ;)
(results might come in handy in unexpected circumstances)
User avatar
Demivec
Addict
Addict
Posts: 4259
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Counting Items in the DataSection?

Post by Demivec »

miso wrote: Wed May 07, 2025 11:16 am
Confused
Don't be. It's a riddle solving game ;)
(results might come in handy in unexpected circumstances)
The situation here reminds me of a children's riddle.

Riddle: Imagine that you are in a room without any doors or windows. How would you get out? :shock:

Answer: Stop imagining. 8)
miso
Enthusiast
Enthusiast
Posts: 409
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Counting Items in the DataSection?

Post by miso »

;)

If you want your datasection items to see, you must answer my riddles of three!

I don't think that anyone here counted his/her datasection items until this topic.
Finding solution is fun, and I'm sure that was the point here.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Counting Items in the DataSection?

Post by #NULL »

You can define your items with a macro and keep counting. Not at compile time though. This depends on the runtime order of execution, so you will have to define your data before you use the count:

Code: Select all

EnableExplicit

Macro DataSectionLabel(_label_)
  DataSection
    _label_:
  EndDataSection
  Define _label_#_itemCount.i = 0
  CompilerIf Not Defined(*dataSection_current_count, #PB_Variable)
    Define *dataSection_current_count.Integer
  CompilerEndIf
  *dataSection_current_count = @_label_#_itemCount
EndMacro

Macro DataSectionData(_data_)
  DataSection
    _data_
  EndDataSection
  *dataSection_current_count\i + 1
EndMacro

DataSectionLabel(label1)
DataSectionData(Data.s "abc")
DataSectionData(Data.s "xyz")
DataSectionLabel(l2)
DataSectionLabel(my_label_3)
DataSectionData(Data.i 111)
DataSectionData(Data.i 222)
DataSectionData(Data.i 222)

Debug label1_itemCount      ; 2
Debug l2_itemCount          ; 0
Debug my_label_3_itemCount  ; 3
You can go further and store the label names (can't use them at Restore though, needs a hardcoded label) and item types (quite an overhead with a type string for every single item, Read also needs a hardcoded type)..

Code: Select all

EnableExplicit

; #########################################################

Structure S_DataSection
  itemCount.i
  List types.s()
EndStructure

Structure S_DataSections
  sectionCount.i
  Map sections.S_DataSection()
EndStructure

Define dataSections.S_DataSections
Define *dataSection_current.S_DataSection

Macro dq
  "
EndMacro

Macro DataSectionLabel(_label_)
  DataSection
    _label_:
  EndDataSection
  dataSections\sectionCount + 1
  *dataSection_current = AddMapElement(dataSections\sections(), dq#_label_#dq)
EndMacro

Macro DataSectionData(_type_, _data_)
  DataSection
    Data._type_ _data_
  EndDataSection
  *dataSection_current\itemCount + 1
  AddElement(*dataSection_current\types())
  *dataSection_current\types() = dq#_type_#dq
EndMacro

; #########################################################

DataSectionLabel(label1)
DataSectionData(s, "abc")
DataSectionData(s, "xyz")

DataSectionLabel(l2)

DataSectionLabel(my_label_3)
DataSectionData(i, 111)
DataSectionData(b, 2)
DataSectionData(s, "three")
DataSectionData(i, 444)

DataSectionLabel(l4)

; #########################################################

Debug "sections: " + dataSections\sectionCount
ForEach dataSections\sections()
  Debug "    " + MapKey(dataSections\sections()) + " (" + dataSections\sections()\itemCount + " items)"
Next
Debug ""

Define i.i
Define b.b
Define s.s

Debug "my_label_3 (" + dataSections\sections("my_label_3")\itemCount + " items) :"
Restore my_label_3
ForEach dataSections\sections("my_label_3")\types()
  Select dataSections\sections("my_label_3")\types()
      Case "i" : Read.i i : Debug "    " + dataSections\sections("my_label_3")\types() + " : " + i
      Case "b" : Read.b b : Debug "    " + dataSections\sections("my_label_3")\types() + " : " + b
      Case "s" : Read.s s : Debug "    " + dataSections\sections("my_label_3")\types() + " : " + s
    EndSelect
Next

; #########################################################

;   sections: 4
;       my_label_3 (4 items)
;       l2 (0 items)
;       l4 (0 items)
;       label1 (2 items)
;   
;   my_label_3 (4 items) :
;       i : 111
;       b : 2
;       s : three
;       i : 444

User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Counting Items in the DataSection?

Post by Blue »

AZJIO wrote: Mon May 05, 2025 10:42 pm
Blue wrote: Sun May 04, 2025 8:55 pm Why complicate simple things ?
[...]
So you shouldn't be underestimating the examples.
[...]
No underestimating. :shock:
I think you're missing the point here.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Post Reply