Synthetic variable assignment -- this[String+number] (IntDict)

Hello,

i suppose it’s a newb question and sorry for that.
i don’t know how to qualify in english (no more in french)…
a composite variable name, a synthetic variable name???

in js your can assign a “synthetic” var name like this
this["toto_"+number] = number;

how can we do in Processing
assign a variable’s name by concatenating String + variable

Is it possible?

regards

éric

Part 1

this works when you say "toto_"+str(number)

Part 2

you can say int gen1=178;

When you want to use a String ("toto_"+str(number)) as an Index or to retrieve a value, look at HashMap:

okay i try to be more clear.

int r1,r2,r3,r4;
void controllerChange(int channel, int number, int value) {
  // number is 31-35, i would like to use "number" to assign the good variable r1 or r2 or r3,,,,
  "r"+str(number-30) = value;
  //does not work!
  eval("r"+str(number-31)) = value;
  // no more
}

how?

perhaps have to do in an other way

éric

please use a HashMap as mentioned

Container IntDict:

ok, it’s working as you write, but i do not like this unfriendly syntax for HashMap intialize! :exploding_head:

HashMap<String,Integer> potards = new HashMap<String,Integer>();

void controllerChange(int channel, int number, int value) {
      potards.put("rot_"+str(number-31), value);
      println("potard rotatif ",number-31," : ",potards.get("rot_"+str(number-31)));
}

see gotoloop’s post please for a simpler way

i do, and i sincerely prefer this syntax.

regards

thanks for this.
It was very helpfull

IntDict potards = new IntDict();

void controllerChange(int channel, int number, int value) {
      potards.set("rot_"+str(number-31), value);
      println("potard rotatif ",number-31," : ",potards.get("rot_"+str(number-31)));
}
2 Likes