question about dynamic array and data

Just starting out? Need help? Post your questions and find answers here.
Lonely_Star77
User
User
Posts: 26
Joined: Sat Feb 20, 2021 3:39 pm

question about dynamic array and data

Post by Lonely_Star77 »

hello...

i'm trying to create a 1 dimensional array with 0 as number of cells in it and then dynamically load it with string data from data section using a procedure in a for loop however when i print the data inside the array i only get the last string and not the whole data... i don't understand what i'm doing wrong

here is the code:

Code: Select all

Dim arr.s(0)
Define.s r

Procedure sappend(Array arr.s(1), temp.s)
  Define i = 0
  ReDim arr(0 + i)
  arr(i) = temp
  i+1
EndProcedure

For i = 0 To 2
  Read.s r
  sappend(arr(),r)
Next


For i = 0 To 2
  Debug arr(i)
Next


DataSection
  Data.s "hello", "hi", "goodbye"
EndDataSection
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: question about dynamic array and data

Post by TI-994A »

Lonely_Star77 wrote:...dynamically load it with string data from data section using a procedure in a for loop...
Try it like this:

Code: Select all

Dim arr.s(0)
Define.s r

Procedure sappend(Array arr.s(1), temp.s, notFirstValue)      
  If notFirstValue
    i = ArraySize(arr()) + 1  
    ReDim arr(i)
  EndIf     
  arr(i) = temp 
EndProcedure

For i = 0 To 2
  Read.s r
  sappend(arr(), r, i)
Next

For i = 0 To 2
  Debug arr(i)
Next

DataSection
  Data.s "hello", "hi", "goodbye"
EndDataSection
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
infratec
Always Here
Always Here
Posts: 6871
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: question about dynamic array and data

Post by infratec »

Code: Select all

Procedure sappend(Array arr.s(1), temp.s)
  Define i = 0
  ReDim arr(0 + i)
  arr(i) = temp
  i+1
EndProcedure
You always do ReDim arr(0 + 0), since i is always 0.
You can ignore the i+1, because after EndProcedure the variable i does no longer exist.
You can do it like this if you use Static instead of Define.
But the solution of TI-99A is a better solution.
Lonely_Star77
User
User
Posts: 26
Joined: Sat Feb 20, 2021 3:39 pm

Re: question about dynamic array and data

Post by Lonely_Star77 »

Thank you very much!
infratec
Always Here
Always Here
Posts: 6871
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: question about dynamic array and data

Post by infratec »

You can add simply

Code: Select all

Debug i
to see this
User avatar
mk-soft
Always Here
Always Here
Posts: 5394
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: question about dynamic array and data

Post by mk-soft »

Even though this is possible with arrays, it is better to use a list when adding a lot of individual data in unknown numbers. (Global NewList row.s())

For example when reading a text file.
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
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: question about dynamic array and data

Post by Olli »

Is this code below right ? This allows the coder to waste time ressources to reallocate on every new string.

Code: Select all

Structure Table
 powSize.I
 maxLimit.I
 realLimit.I
 Array A$(0)
EndStructure

Procedure TableCreate()
 Define *This.Table = AllocateMemory(SizeOf(Table) )
 InitializeStructure(*This, Table)
 ProcedureReturn *This
EndProcedure

Procedure TableGrow(*This.Table)
 With *This
  \realLimit + 1
  If \realLimit > \maxLimit
   \powSize + 1
   \maxLimit = 1 << \powSize - 1
   Redim \A$(\maxLimit) ; <<< --- right ?
  EndIf
 EndWith
EndProcedure

;- Main

Define *x = TableCreate()
For i = 0 to 5
 TableGrow(*x)
 *x\A$(i) = Str(i * i)
 Debug *x\A$(i)
Next
Axolotl
Enthusiast
Enthusiast
Posts: 447
Joined: Wed Dec 31, 2008 3:36 pm

Re: question about dynamic array and data

Post by Axolotl »

Hi,
I wouldn't say it's right or wrong. The idea I recognize here is not to always call the REDIM. This saves time and conserves resources.

Whether this approach is sufficiently "correct" depends INMO on the expected result in terms of speed.

An alternative for Arrays are known to be the List and Map functions.

But also in this case probably applies: 10 programmers equal 11 opinions. (10 is not binary. :-))

My little suggestion on your example code, not starting with zero at the member variables:

Code: Select all

Procedure TableCreate(SizeChunks.i = 3) 
  ; ... 
  *This\powSize = SizeChunks 
  *This\maxLimit = 1 << SizeChunks 
EndProcedure 


Happy coding and Stay healthy.
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: question about dynamic array and data

Post by TI-994A »

Olli wrote:...This allows the coder to waste time ressources to reallocate on every new string...
If the intended array size is known beforehand, it would be easier and more productive to simply dimension and assign it all at once. However, if the array size grows gradually with the execution of the program, then it has to be re-dimensioned and assigned as required.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: question about dynamic array and data

Post by Olli »

Replacing 5 with 500, then 5000, then 50000, then 500000 strings in the code line << For i = 0 To 5 >>.
If Redim is okay for dynamic arrays (apparently nobody wanted to check...), you have just 19 array changes. What it is not a lot for 500 000 strings updates... (considering you have only 4 megabytes of wasted strings memory if you cause the grow to a limit at 1 000 000 for 500 001 strings - exactly 2^19+1 strings)
Post Reply