Loading...
Logo
Processing Forum
If I have a variable say, X3; can I construct a way to refer to it mathematically?
i.e. 
byte x3 =0;
byte a=3;
 "X" + a =6

I need to provide the 3 indirectly. 

Replies(4)

You could use a Array or HashMap to store a set of variables like this.  If they are all uniform, and you are accessing by number, I would use an array, which is simpler.

Copy code
  1. int[] x;

  2. void setup()
  3. {
  4.   x = new int[3];
  5.   x[0] = 3;
  6.   x[1] = 26;
  7.   x[2] = 64;
  8.   frameRate(1);
  9. }

  10. void draw()
  11. {
  12.   int i = (int) random(0,3);
  13.   println("X"+i + " equals " + x[i]);
  14. }



Thank you and I see where you are going, however it does not 'seem' to fit the need.
The actual use is for colours that are unrelated mathematically...
as shown in the help. 

color c1 = color(204, 153, 0);
color c2
color c3 etc

I then wanted to select these color (RGBs) by the contents of an existing array containing 1, 2 or 3 in no sequence.

i.e.  fill ("c"+     ( myarray[x][y]==1) ? 2:1);  or even

int num =  ( myarray[x][y]==1) ? 2:1;
fill("c"+ asc(num))  or something to produce c1 or c2 etc.

I saw the possibility in Delphi some time ago but cannot now find the method or what it was called; for mathematically generating a variable name.

Can you help further please?
No really, use an array.

Copy code
  1. color[] c = { color(204,153,0), color(0,0,0), color(0,0,255) };
  2. c[2] = color(255,0,0);

  3. fill( c[ (myArray[x][y]==1)?2:1 ] );

  4. int num = (myArray[x][y]==1)?2:1;
  5. fill( c[num] );
Thank you both very much for that solution, dare I say elegant solution. I couldn't connect the first suggestion with my precise need.