[Solved] Could macro parameter be used as a string?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

[Solved] Could macro parameter be used as a string?

Post by Michael Vogel »

I am using a simple macro to convert C declarations of arrays easily to PureBasic and want to debug the content of an array. Is there a possibility to do an output of the name of the array in the ShowArray macro without putting the name (NUM_ERROR_CORRECTION_CODEWORDS in the example below) in quotes?

Code: Select all

Macro CreateArray(name,vals)

	Global _1=CountString(vals,"{")-1
	Global _2=CountString(vals,",")-_1+1
	Global _i,_j,_s.s

	If _1<1 Or _1<>CountString(vals,"}")-1 Or _2%_1
		Debug "Dimension error "+Str(_1)+" x "+Str(_2/_1)
	Else
		_2/_1
		_1-1
		Global Dim name(_1,_2)
		For _i=0 To _1
			_s=Left(",",Bool(_i=0))+ReplaceString(StringField(vals,_i+1,"}"),"{","")
			For _j=0 To _2
				name(_i,_j)=Val(Trim(StringField(_s,_j+2,",")))
				Debug Str(_i)+","+Str(_j)+" = "+Str(name(_i,_j))
			Next _j
		Next _i
	EndIf

EndMacro
Macro ShowArray(name)

	Global _1=ArraySize(name(),1)
	Global _2=ArraySize(name(),2)
	Global _i,_j,_s.s

	Debug "Array "+name+"("+Str(_i)+","+Str(_j)+"):"
	For _i=0 To _1
		_s="{"+Left("{",Bool(_i=0))
		For _j=0 To _2
			_s+Str(name(_i,_j))+Left(",",Bool(_j-_2))
		Next _j
		_s+"}"+Mid(",}",Bool(_i=_1)+1,1)
		Debug _s
	Next _i

EndMacro

CreateArray(NUM_ERROR_CORRECTION_CODEWORDS,"{{ 10, 16, 26, 36, 48,  64,  72,  88, 110, 130, 150, 176},{  7, 10, 15, 20, 26,  36,  40,  48,  60,  72,  80,  96},{ 17, 28, 44, 64, 88, 112, 130, 156, 192, 224, 264, 308},{ 13, 22, 36, 52, 72,  96, 108, 132, 160, 192, 224, 260}}")
ShowArray(NUM_ERROR_CORRECTION_CODEWORDS)
Last edited by Michael Vogel on Sun Nov 18, 2018 12:05 pm, edited 1 time in total.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Could macro parameter be used as a string?

Post by #NULL »

Code: Select all

Macro dq
 "
EndMacro
 
Macro ShowArray(name)

   [...]

   Debug "Array "+dq#name#dq+"("+Str(_i)+","+Str(_j)+"):"
   
   [...]

User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Could macro parameter be used as a string?

Post by Michael Vogel »

Brilliant, #Null!

Didn't believe that there's a solution, but you got it. Here's another one, but here I'm 100% sure, it can't be solved...

The result of the following code seems to handle arrays with one or two dimensions, but in fact I am creating two-dimensional arrays in both cases, otherwise the CreateArray macro would fail. Any chance to do a Dim name(_2) for vectors and a Dim name(_1,_2) for matrices just within a single macro?

Code: Select all

Macro _Q
	"
EndMacro
Macro CreateArray(name,vals)

	Global _1=CountString(vals,"{")-1
	Global _2=CountString(vals,",")-_1+1
	Global _i,_j,_s.s

	If _1=0
		_2-1
		Global Dim name(_2,#Null);	(_2)
		_s=ReplaceString(vals,"{","")
		For _i=0 To _2
			name(_i,#Null)=Val(Trim(StringField(_s,_i+1,",")));	(_i)
		Next _i

	ElseIf _1<>CountString(vals,"}")-1 Or _2%_1
		Debug "Dimension error "+Str(_1)+" x "+Str(_2/_1)

	Else
		_2/_1
		_1-1
		Global Dim name(_1,_2)
		For _i=0 To _1
			_s=Left(",",Bool(_i=0))+ReplaceString(StringField(vals,_i+1,"}"),"{","")
			For _j=0 To _2
				name(_i,_j)=Val(Trim(StringField(_s,_j+2,",")))
				;Debug Str(_i)+","+Str(_j)+" = "+Str(name(_i,_j))
			Next _j
		Next _i
	EndIf

EndMacro
Macro ShowArray(name)

	Global _1=ArraySize(name(),1)
	Global _2=ArraySize(name(),2)
	Global _i,_j,_s.s

	If _2
		Debug "Array "+_Q#name#_Q+"("+Str(_1+1)+","+Str(_2+1)+"):"
	Else
		Debug "Array "+_Q#name#_Q+"("+Str(_1+1)+"):"
	EndIf

	_s="{"
	For _i=0 To _1
		If _2 : _s="{"+Left("{",Bool(_i=0)) : EndIf
		For _j=0 To _2
			_s+Str(name(_i,_j))+Left(",",Bool(_j-_2))
		Next _j
		_s+Left("}",_2)+Mid(",}",Bool(_i=_1)+1,1)
		If _2 Or _i=_1
			Debug _s
		EndIf
	Next _i

EndMacro

CreateArray(Matrix,"{{1,2,3},{4,5,6}}")
CreateArray(Vector,"{1,2,3}")

ShowArray(Matrix)
ShowArray(Vector)

Last edited by Michael Vogel on Sun Nov 18, 2018 12:02 pm, edited 1 time in total.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Could macro parameter be used as a string?

Post by #NULL »

You need the number of dimensions at compile-time to define the array, so you won't be able to deduce it from any string which can only be processed at run-time. Maybe use an additional parameter like CreateArray(Matrix, 2,"{{1,2,3},{4,5,6}}") with a CompilerIf/Select to branch into the right array definition and maybe also check if the data string nesting matches the specified dimension. But there is no PB ArrayDimensions() like there is ArraySize(). So for ShowArray() you either need to pass the dimensions too, or store it separately like in a map of array names.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Could macro parameter be used as a string?

Post by #NULL »

There is also ExtractJSONArray() etc. to unstring your string. :)
btw. your code tag contains duplicate code, was confusing for a moment.
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Could macro parameter be used as a string?

Post by Michael Vogel »

Thanks, I made two macros now, a CreateVector and another CreateMatrix - not what I wanted, but good enough...

PS: I've removed the duplicate code now, must have pressed Ctrl+V to often :wink:
Post Reply