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

**URL:** <https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267>\
**Category:** Beginners\
**Created:** [December 23, 2021, 3:33pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267 "2021-12-23T15:33:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbbp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrbbp/32/15313_2.png) [@mrbbp](https://discourse.processing.org/u/mrbbp)\
**Post date:** [December 23, 2021, 3:33pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267/1 "2021-12-23T15:33:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 23, 2021, 3:39pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267/2 "2021-12-23T15:39:08Z")

</div>

**Part 1**

> [@mrbbp](#):
>
> “toto\_”+number

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

**Part 2**

> [@mrbbp](#):
>
> assign a variable’s name by concatenating String + variable

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:

> **[HashMap / Reference](https://processing.org/reference/HashMap.html)**
>
> A HashMap stores a collection of objects, each referenced by a key. This is similar to an Array, only instead of accessing elements with a numeric index, a String is used. (If you…

---

<div class="post-metadata">

**Author:** ![mrbbp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrbbp/32/15313_2.png) [@mrbbp](https://discourse.processing.org/u/mrbbp)\
**Post date:** [December 23, 2021, 3:47pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267/3 "2021-12-23T15:47:51Z")

</div>

okay i try to be more clear.

```auto
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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 23, 2021, 3:55pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267/4 "2021-12-23T15:55:51Z")

</div>

please use a HashMap as mentioned

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 23, 2021, 4:04pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267/5 "2021-12-23T16:04:00Z")

</div>

Container IntDict:

> **[Reference](https://processing.org/reference/IntDict.html)**
>
> A simple class to use a String as a lookup for an int value. String "keys" are associated with integer values.

---

<div class="post-metadata">

**Author:** ![mrbbp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrbbp/32/15313_2.png) [@mrbbp](https://discourse.processing.org/u/mrbbp)\
**Post date:** [December 23, 2021, 4:04pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267/6 "2021-12-23T16:04:11Z")

</div>

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

```auto
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)));
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 23, 2021, 4:05pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267/7 "2021-12-23T16:05:14Z")

</div>

> [@mrbbp](#):
>
> but i do not like this unfriendly syntax for HashMap intialize

see gotoloop’s post please for a simpler way

---

<div class="post-metadata">

**Author:** ![mrbbp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrbbp/32/15313_2.png) [@mrbbp](https://discourse.processing.org/u/mrbbp)\
**Post date:** [December 23, 2021, 4:09pm UTC](https://discourse.processing.org/t/synthetic-variable-assignment-this-string-number-intdict/34267/8 "2021-12-23T16:09:33Z")

</div>

i do, and i sincerely prefer this syntax.

regards

thanks for this.  
It was very helpfull

```auto
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)));
}

```
