Array of lists

Just starting out? Need help? Post your questions and find answers here.
tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Array of lists

Post by tua »

How does one define an array of lists when the size of the array is only known @ runtime?
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Array of lists

Post by jacdelad »

Code: Select all

Structure Lists
  MyListDefinition.l
EndStructure

Define NewMap ListMap.Lists()

Debug @ListMap("List1")
Debug @ListMap("List2")
One of many solutions. Each map element contains a list with the definition of your choice.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2227
Joined: Sun May 14, 2017 1:48 am

Re: Array of lists

Post by AZJIO »

I didn't understand the question, but I made an example with an array of lists.
What do you want to define?

Code: Select all

EnableExplicit

Structure y
  List x.i()
EndStructure

Define Dim Arr.y(0)
Define i

AddElement(Arr(0)\x())
Arr(0)\x() = 1
AddElement(Arr(0)\x())
Arr(0)\x() = 2

ReDim Arr(1)

AddElement(Arr(1)\x())
Arr(1)\x() = 11
AddElement(Arr(1)\x())
Arr(1)\x() = 22

For i = 0 To ArraySize(Arr())
	ForEach Arr(i)\x()
		Debug Arr(i)\x()
	Next
Next
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Array of lists

Post by Demivec »

tua wrote: Sat Jan 06, 2024 4:29 am How does one define an array of lists when the size of the array is only known @ runtime?

Code: Select all

Structure item_list
  List items.s()
EndStructure

Define lists_needed

lists_needed = Random(12, 1)  ;number of lists defined at runtime

Dim b.item_list(lists_needed - 1)  ;zero based size

Debug ArraySize(b())

Repeat:
   lists_needed = Random(12, 1)  ; change number of lists defined at runtime
 Until lists_needed <> ArraySize(b() + 1)
 
 ReDim b.item_list(lists_needed - 1) ;or  Dim b.item_list(lists_needed) to clear / free all previously existing lists

Debug ArraySize(b())
Does that answer your question? The example can be extended to populate the example lists and display their contents if it is needed.
tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Re: Array of lists

Post by tua »

thanks to all - exactly what I was looking for
Post Reply