POV-Ray : Newsgroups : povray.general : Array in function. Not, but... : Re: Array in function. Not, but... Server Time
14 Jun 2021 17:20:25 EDT (-0400)
  Re: Array in function. Not, but...  
From: Cousin Ricky
Date: 14 Apr 2021 18:16:44
Message: <607769cc@news.povray.org>
On 2021-04-14 1:50 PM (-4), ingo wrote:
> As we can't use arrays inside functions I tried something else. A spline 
> is an array, kind of and works in functions. So some test code, that 
> works. There's one little thing I don't realy understand. I have to undef 
> the 'i' used in SPL or rename it or the one in N. Otherwise I get: 
> 
> line xxx: Parse Error: Local variable name expected!
> 
> ---%<------%<------%<---
> #declare arrSPL = array[5]{
>   <0,0>,
>   <1,0>,
>   <2,0>,
>   <3,0>,
>   <4,0>
> }  
>   
> #declare SPL = spline{
>   #for(i,0,dimension_size(arrSPL,1)-1)
>     i, arrSPL[i],
>   #end
> }
> #debug concat(str(SPL(4).x,5,5),"\n")
> #undef arrSPL
> #undef i      // <--------- !!
> 
> #declare Ar = function{
>   spline{SPL}
> }
> #debug concat(str(Ar(4).x,5,5),"\n")
> #undef SPL
> 
> #declare N = function(D){sum(i, 0, D, Ar(i).x)};
> #declare NAR = N(4);
> #debug concat(str(NAR,5,5),"\n")
> ---%<------%<------%<---

You've just been burned by POV-Ray's scope leakage, a design flaw IMO.
It has nothing to do with you having used i to define the spline.  *Any*
identifier name local to a function definition will fail if the name had
been previously used--even if it was declared in an include file that
you did not write.  You will also get a parse error if you #declare D at
the top of your scene file.

I make it a practice never to use a name in a function that is used
elsewhere in the scene.  Since this means avoiding names declared in
include files written by other people, I have to get creative with
identifier names.

Note that you did not need to #undef arrSPL or SPL.  The scene runs fine
without those 2 #undefs.


Post a reply to this message

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