POV-Ray : Newsgroups : povray.general : #ifdef using a string expression? : Re: #ifdef using a string expression? Server Time
9 May 2024 10:03:05 EDT (-0400)
  Re: #ifdef using a string expression?  
From: Kenneth
Date: 13 Apr 2023 13:25:00
Message: <web.64383a5198df09c99b4924336e066e29@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> "Kenneth" <kdw### [at] gmailcom> wrote:
>
> >   #ifdef(AA & BB)
> > Any suggestions or comments?
>
> Depending on how many variables you need to test, you lump them all together
> into an array and cycle through them, using the product of the result
> as a sort of Boolean AND result.
>
>
> #declare AA = 1;
> #declare BB = 3;
>
> #declare Variables = array {AA, BB}
>
> #macro And (_Array)
>  #declare Elements = dimension_size (_Array, 1)-1;
>  #declare AndResult = 1;
>  #for (Index, 0, Elements)
>   #declare AndResult = AndResult * defined (_Array[Index]);
>  #end
>  AndResult
> #end
>
>
> #if (And (Variables))
>  #debug "All variables are defined. \n"
> #end
>

I had to think about this one (and had to take a temporary break because of all
of the new Spring pollen in the air, here in the South. Sneezing uncontrollably!
Me no likey!)

This is an interesting way of attacking the problem, and works fine as-is. But
if I add an UN-declared variable to the array (i.e., wondering if I had
previously declared it or not), this line fails:
           #declare Variables = array {AA, BB, CC}

"Cannot pass uninitialized identifier to non-optional array initializer."
(What a mouthful!)

But I'm probably going outside the scope of my simpler  #ifdef(AA & BB)
question. JR's #if/#else code allows a more graceful exit though: I could add a
#debug like, "One or more of your variables is undefined."


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.