Macro for single Array with Values

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 6251
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Macro for single Array with Values

Post by mk-soft »

I experimented once with macros and arrays

Code: Select all

;-Begin

; Comment: Macros for Arrays
; Author : mk-soft
; Version: v1.01
; Created: 13.05.2016
; Updated: 
; Link   : 

; ***************************************************************************************

Macro DimV(name, size, v0=0, v1=0, v2=0, v3=0, v4=0, v5=0, v6=0, v7=0, v8=0, v9=0, v10=0, v11=0, v12=0, v13=0, v14=0, v15=0, v16=0, v17=0, v18=0, v19=0)
  Dim name(size)
  CompilerIf v0 : name(0) = v0 : CompilerEndIf
  CompilerIf v1 : name(1) = v1 : CompilerEndIf
  CompilerIf v2 : name(2) = v2 : CompilerEndIf
  CompilerIf v3 : name(3) = v3 : CompilerEndIf
  CompilerIf v4 : name(4) = v4 : CompilerEndIf
  CompilerIf v5 : name(5) = v5 : CompilerEndIf
  CompilerIf v6 : name(6) = v6 : CompilerEndIf
  CompilerIf v7 : name(7) = v7 : CompilerEndIf
  CompilerIf v8 : name(8) = v8 : CompilerEndIf
  CompilerIf v9 : name(9) = v9 : CompilerEndIf
  CompilerIf v10 : name(10) = v10 : CompilerEndIf
  CompilerIf v11 : name(11) = v11 : CompilerEndIf
  CompilerIf v12 : name(12) = v12 : CompilerEndIf
  CompilerIf v13 : name(13) = v13 : CompilerEndIf
  CompilerIf v14 : name(14) = v14 : CompilerEndIf
  CompilerIf v15 : name(15) = v15 : CompilerEndIf
  CompilerIf v16 : name(16) = v16 : CompilerEndIf
  CompilerIf v17 : name(17) = v17 : CompilerEndIf
  CompilerIf v18 : name(18) = v18 : CompilerEndIf
  CompilerIf v19 : name(19) = v19 : CompilerEndIf
EndMacro

Macro DimS(name, size, s0="", s1="", s2="", s3="", s4="", s5="", s6="", s7="", s8="", s9="", s10="", s11="", s12="", s13="", s14="", s15="", s16="", s17="", s18="", s19="")
  Dim name.s(size)
  CompilerIf Bool(s0<>"") : name(0) = s0 : CompilerEndIf
  CompilerIf Bool(s1<>"") : name(1) = s1 : CompilerEndIf
  CompilerIf Bool(s2<>"") : name(2) = s2 : CompilerEndIf
  CompilerIf Bool(s3<>"") : name(3) = s3 : CompilerEndIf
  CompilerIf Bool(s4<>"") : name(4) = s4 : CompilerEndIf
  CompilerIf Bool(s5<>"") : name(5) = s5 : CompilerEndIf
  CompilerIf Bool(s6<>"") : name(6) = s6 : CompilerEndIf
  CompilerIf Bool(s7<>"") : name(7) = s7 : CompilerEndIf
  CompilerIf Bool(s8<>"") : name(8) = s8 : CompilerEndIf
  CompilerIf Bool(s9<>"") : name(9) = s9 : CompilerEndIf
  CompilerIf Bool(s10<>"") : name(10) = s10 : CompilerEndIf
  CompilerIf Bool(s11<>"") : name(11) = s11 : CompilerEndIf
  CompilerIf Bool(s12<>"") : name(12) = s12 : CompilerEndIf
  CompilerIf Bool(s13<>"") : name(13) = s13 : CompilerEndIf
  CompilerIf Bool(s14<>"") : name(14) = s14 : CompilerEndIf
  CompilerIf Bool(s15<>"") : name(15) = s15 : CompilerEndIf
  CompilerIf Bool(s16<>"") : name(16) = s16 : CompilerEndIf
  CompilerIf Bool(s17<>"") : name(17) = s17 : CompilerEndIf
  CompilerIf Bool(s18<>"") : name(18) = s18 : CompilerEndIf
  CompilerIf Bool(s19<>"") : name(19) = s19 : CompilerEndIf
EndMacro

; ***************************************************************************************

CompilerIf #PB_Compiler_IsMainFile
  Debug "Array Of Numbers"
  Global DimV(daten.f, 3, 200.6, 300.3, 99.1)
  
  Define index
  For index = 0 To ArraySize(Daten())
    Debug daten(index)
  Next
  
  Debug "Array Of String"
  Global DimS(Wochentag, 6, "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag")
  Define index
  For index = 0 To ArraySize(Wochentag())
    Debug Wochentag(index)
  Next
CompilerEndIf
:wink:
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
idle
Always Here
Always Here
Posts: 5914
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Macro for single Array with Values

Post by idle »

That fits the syntax quite nicely. :D

a lower level alternative is to use an iterator and data section.

Code: Select all

Structure ArrayITR   ;generic 1D array itterator 
  StructureUnion 
    a.a[0]
    b.b[0]
    c.c[0]
    u.u[0]
    w.w[0]
    l.l[0]
    f.f[0]
    i.i[0]
    q.q[0]
    d.d[0]
  EndStructureUnion 
  s.s[0]
EndStructure   

DataSection   ;datasection can be declared anywhere 
QuadArray:  
Data.q 1,2,3,4,5,6  
ByteArray:
Data.b 9,8,7,6,5,4 
FloatArray:
Data.f 1.1,1.2,1.3,1.4,1.5,1.6
StringArray: 
Data.i @"One",@"Two",@"Three",@"Four",@"Five",@"Six"
EndDataSection 

*itr.ArrayITR = ?QuadArray ;set address of iterator 

For a = 0 To 5 
  Debug *itr\q[a] 
Next   
Debug "-----"

*itr = ?ByteArray 

For a = 0 To 5 
  Debug *itr\b[a] 
Next   
Debug "-----"

*itr = ?FloatArray 

For a = 0 To 5 
  Debug *itr\f[a] 
Next   
Debug "-----"

*itr = ?QuadArray 

For a = 0 To 5 
  *itr\q[a] = 6-a  ;overwrite quad array, reversing it  
Next   

For a = 0 To 5 
  Debug *itr\q[a] 
Next   
Debug "-----"

*itr = ?StringArray 

For a = 0 To 5 
  Debug *itr\s[a]  
Next  

Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 6251
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Macro for single Array with Values

Post by mk-soft »

That´s right.

Another way is to create a DataSet-Object (http://www.purebasic.fr/english/viewtop ... 05#p478750)
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
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Macro for single Array with Values

Post by davido »

@mk-soft,
Very interesting example.

@idle,
Another interesting and rather unexpected method.

I often thank Fred for not implementing all requests since it produces a plethora of ingenious examples to circumvent them! :)
DE AA EB
Axolotl
Addict
Addict
Posts: 838
Joined: Wed Dec 31, 2008 3:36 pm

Re: Macro for single Array with Values

Post by Axolotl »

Hi mk-soft,

nice idea.
I tried some thoughts....

possible to use this without the size argument like this

Code: Select all

Macro DimTest(name, v0=0, v1=0, v2=0, v3=0, v4=0, v5=0, v6=0, v7=0, v8=0, v9=0, v10=0, v11=0, v12=0, v13=0, v14=0, v15=0, v16=0, v17=0, v18=0, v19=0)
  Dim name(0)
  CompilerIf v0 : ReDim name(0) : name(0) = v0 : CompilerEndIf
  CompilerIf v1 : ReDim name(1) : name(1) = v1 : CompilerEndIf
  CompilerIf v2 : ReDim name(2) : name(2) = v2 : CompilerEndIf
  ;...
EndMacro
To use the redim to add more values to the array...
unfortunately I cannot get rid of the size argument than

Code: Select all

Macro ReDimTest(name, size, v0=0, v1=0, v2=0, v3=0, v4=0, v5=0, v6=0, v7=0, v8=0, v9=0, v10=0, v11=0, v12=0, v13=0, v14=0, v15=0, v16=0, v17=0, v18=0, v19=0)
  ReDim name(size)
  CompilerIf v0 : ReDim name(Size+0) : name(Size+0) = v0 : CompilerEndIf
  CompilerIf v1 : ReDim name(Size+1) : name(Size+1) = v1 : CompilerEndIf
  CompilerIf v2 : ReDim name(Size+2) : name(Size+2) = v2 : CompilerEndIf
  CompilerIf v3 : ReDim name(Size+3) : name(Size+3) = v3 : CompilerEndIf
  CompilerIf v4 : ReDim name(Size+4) : name(Size+4) = v4 : CompilerEndIf
  ;...
EndMacro
because of the arraysize(myVar()) statement...

Code: Select all

  Global DimTest(myVar, 1, 2, 3)
  Define index
  Define size = ArraySize(myVar())+1

  ReDimTest(myVar, size, 12, 34, 56, 78)
  For index = 0 To ArraySize(myVar())
    Debug Str(index)+".  = "+myVar(index)
  Next
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply