POV-Ray : Newsgroups : povray.newusers : conditional statements Server Time
23 Dec 2024 15:15:08 EST (-0500)
  conditional statements (Message 1 to 9 of 9)  
From: mysdn
Subject: conditional statements
Date: 17 Jul 2009 04:50:00
Message: <web.4a603abbcc998528ff025eb50@news.povray.org>
Hi,

is it possible to write

 #if (DoorModel == model_101)   in an if statement

and  in switches


  #switch(DoorModel)

           #case (model_101)// do that

I want to test for strings, is ttah possible in pov.

thank you


Post a reply to this message

From: Chris B
Subject: Re: conditional statements
Date: 17 Jul 2009 05:17:40
Message: <4a6041b4$1@news.povray.org>
"mysdn" <kam### [at] hotmailcom> wrote in message 
news:web.4a603abbcc998528ff025eb50@news.povray.org...
> Hi,
>
> is it possible to write
>
> #if (DoorModel == model_101)   in an if statement
>
> and  in switches
>
>
>  #switch(DoorModel)
>
>           #case (model_101)// do that
>
> I want to test for strings, is ttah possible in pov.
>

Yes.
You can test strings in both #if and #case statements, although the syntax 
is a little contrived in #case statements.

You can use the 'strcmp' function in #if statements which compares two 
strings and returns zero if the two strings are identical. So:

  #if (strcmp(DoorModel, "model_101")=0)

compares a string contained in the variable DoorModel with the literal 
scting value "model_101".


In the #case statement the value in the #switch has to be numeric, so you 
have to construct something like:

#switch (0)
  #case(strcmp(DoorModel, "model_101"))
    ...
  #break
  #case(strcmp(DoorModel, "model_102"))
    ...
  #break
  #else
    ...
#end

I also often use the 'strlwr' function to desensitize such string 
comparisons to upper and lower case.
BTW. You can also use the 'substr' and 'strlen' functions to make it accept 
abbreviations of strings during a comparison.
But this can end up with quite long expressions in the #case statement.


Regards,
Chris B.


Post a reply to this message

From: clipka
Subject: Re: conditional statements
Date: 17 Jul 2009 05:50:01
Message: <web.4a6048b86dc58f70a4aed1130@news.povray.org>
"mysdn" <kam### [at] hotmailcom> wrote:
> is it possible to write
>
>  #if (DoorModel == model_101)   in an if statement

In POV 3.6, you can achieve this using:

  #if (strcmp(DoorModel,model_101)) ...

In POV 3.7, POV-Ray's comparison operator works for strings, too, so you can
also use:

  #if (DoorModel = model_101) ...

(Note the single equals sign; POV-Ray does *not* use C/C++ style operators.)


> and  in switches
>
>   #switch(DoorModel)
>
>            #case (model_101)// do that
>
> I want to test for strings, is ttah possible in pov.

No, that's only possible with float values, even in 3.7.

Out of curiosity: Are you sure you want to use strings for the model
identifiers? A numeric id would do, I guess. After all, you can always use
those identifiers you have, like model_101, so you don't have to memorize all
the - possibly cryptic - ids.

If you absolutely need to work with strings for model identifiers, I suggest
translating them to numerical ids first, by looking them up in an array.


Post a reply to this message

From: clipka
Subject: Re: conditional statements
Date: 17 Jul 2009 06:15:00
Message: <web.4a604edd6dc58f70a4aed1130@news.povray.org>
"Chris B" <nom### [at] nomailcom> wrote:
> #switch (0)
>   #case(strcmp(DoorModel, "model_101"))
>     ...
>   #break
>   #case(strcmp(DoorModel, "model_102"))
>     ...
>   #break
>   #else
>     ...
> #end

Ah, now *that's* an interesting idea... thanks, even I still live and learn.
I've always missed an #if ... #elseif construct in POV-Ray, but looks like this
is it.

BTW, in POV-Ray 3.7 it should also work with the comparison operator, i.e.:

  #case(DoorModel = "model_101")


Post a reply to this message

From: Chris B
Subject: Re: conditional statements
Date: 17 Jul 2009 06:58:51
Message: <4a60596b$1@news.povray.org>
"clipka" <nomail@nomail> wrote in message 
news:web.4a604edd6dc58f70a4aed1130@news.povray.org...
> "Chris B" <nom### [at] nomailcom> wrote:
>> #switch (0)
>>   #case(strcmp(DoorModel, "model_101"))
>>     ...
>>   #break
>>   #case(strcmp(DoorModel, "model_102"))
>>     ...
>>   #break
>>   #else
>>     ...
>> #end
>
> Ah, now *that's* an interesting idea... thanks, even I still live and 
> learn.
> I've always missed an #if ... #elseif construct in POV-Ray, but looks like 
> this
> is it.
>
> BTW, in POV-Ray 3.7 it should also work with the comparison operator, 
> i.e.:
>
>  #case(DoorModel = "model_101")
>

Well that's good news. That simplifies the syntax. I'll have to give that a 
go. It's worth noting that you'd presumably pass '1' or 'true' into the 
#switch statement rather than '0' using this new syntax.

Regards,
Chris B.


Post a reply to this message

From: Warp
Subject: Re: conditional statements
Date: 17 Jul 2009 07:29:12
Message: <4a606088@news.povray.org>
clipka <nomail@nomail> wrote:
> In POV 3.6, you can achieve this using:

>   #if (strcmp(DoorModel,model_101)) ...

  Actually you have to do it like:

#if(strcmp(DoorModel, model_101) = 0) ...

-- 
                                                          - Warp


Post a reply to this message

From: clipka
Subject: Re: conditional statements
Date: 17 Jul 2009 08:05:00
Message: <web.4a6067d16dc58f70a4aed1130@news.povray.org>
"Chris B" <nom### [at] nomailcom> wrote:
> Well that's good news. That simplifies the syntax. I'll have to give that a
> go. It's worth noting that you'd presumably pass '1' or 'true' into the
> #switch statement rather than '0' using this new syntax.

Whoops - yes, of course.


Post a reply to this message

From: clipka
Subject: Re: conditional statements
Date: 17 Jul 2009 08:15:01
Message: <web.4a606b2e6dc58f70a4aed1130@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> >   #if (strcmp(DoorModel,model_101)) ...
>
>   Actually you have to do it like:
>
> #if(strcmp(DoorModel, model_101) = 0) ...

Yup. Revoco.


Post a reply to this message

From: Alain
Subject: Re: conditional statements
Date: 17 Jul 2009 22:25:51
Message: <4a6132af$1@news.povray.org>

> Hi,
> 
> is it possible to write
> 
>  #if (DoorModel == model_101)   in an if statement
> 
> and  in switches
> 
> 
>   #switch(DoorModel)
> 
>            #case (model_101)// do that
> 
> I want to test for strings, is ttah possible in pov.
> 
> thank you
> 
> 
If all your identifiers are of the same format as model_xxx, then, you 
can extract the numerical part and test that as a numeric value.

Or

You can construct your identifiers from a "model_" string concatenated 
to the numerical part.

In both cases, you'd only need to compare numeric variables, whitch is 
much simpler.



Alain


Post a reply to this message

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