# How to use a variable name that's stored inside the variable? Reflection?

**URL:** <https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730>\
**Category:** Libraries\
**Created:** [September 22, 2018, 7:32am UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730 "2018-09-22T07:32:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luka](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@Luka](https://discourse.processing.org/u/Luka)\
**Post date:** [September 22, 2018, 7:32am UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730/1 "2018-09-22T07:32:31Z")

</div>

I’m trying to control a sketch through OSC from another application (SuperCollider). I want to be able to pass control data (integers) to various variables in the sketch by naming them in the OSC message.

For example I want to send an OSC message `/var/backgroundHue/123` upon which the sketch would directly assign integer `123` to a variable `backgroundHue`.

I want to avoid specifying **each** variable like this:

```auto
if(theOscMessage.checkAddrPattern("/var")==true) {
    if(oscVal1.equals( "backgroundHue")) { 
        backgroundHue = oscVal2; 
    }
}

```

Is this possible? I found something about reflection as a way to get information about a variable, but not sure how to use that.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [September 22, 2018, 8:59am UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730/2 "2018-09-22T08:59:55Z")

</div>

can not help with this, but a other way to deal with incoming ?lists?  
would be JSON

```auto
JSONArray OSCvalues;
JSONObject setting;

//test data
String OSCval1="backgroundHue";
int OSCval2=123;

void setup() {
  OSCvalues = new JSONArray();
  setting = new JSONObject();
  incoming();
}

void incoming() {
  // setting.setInt("id",1);
  setting.setString("parameter", OSCval1);
  setting.setInt("value", OSCval2);

  OSCvalues.setJSONObject(0, setting);

  saveJSONArray(OSCvalues, "data/OSC.json");

  println(OSCvalues);
}

```

---

<div class="post-metadata">

**Author:** ![Luka](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@Luka](https://discourse.processing.org/u/Luka)\
**Post date:** [September 22, 2018, 9:51am UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730/3 "2018-09-22T09:51:24Z")

</div>

putting aside that there’s no added value in storing incoming data - they are better off being sent immediately to the function that is animating stuff in realtime - but even if stored, how do you interpret the “parameter” string from JSON file, as variable name so that you could assign a value vrom OSCval2 to it? like

```auto
${parameter} = value;

```

(${} syntax is invented here as a pseudo code)

can a string be interpreted as variable name?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 22, 2018, 11:12am UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730/4 "2018-09-22T11:12:37Z")

</div>

Reflection is not an easy topic to implement. An easier solution might be to use a hashmap to create a dictionary of ‘pseudo variables’. The following sketch shows how this might be done with mixed data types. Simply click the mouse to change the pseudo-variable values.

```auto
HashMap<String, Object> variables = new HashMap<String, Object>();

void setup() {
  size(400, 400);
  changeVariables();
}

void draw() {
  background(32);
  float x0 = (float) variables.get("x0");
  float y0 = (float) variables.get("y0");
  float x1 = (float) variables.get("x1");
  float y1 = (float) variables.get("y1");
  float strokeWeight = (float) variables.get("weight");
  int colour = (int) variables.get("colour");
  String label0 = (String) variables.get("label0");
  String label1 = (String) variables.get("label1");
  strokeWeight(strokeWeight);
  stroke(colour);
  line(x0, y0, x1, y1);
  fill(255);
  text(label0, x0+6, y0-10);
  text(label1, x1+6, y1-10);
}

void mouseClicked() {
  changeVariables();
}

void changeVariables() {
  float x0 = random(width);
  float y0 = random(height);
  float x1 = random(width);
  float y1 = random(height);
  String label0 = "[" + round(x0) + "," + round(y0) + "]";
  String label1 = "[" + round(x1) + "," + round(y1) + "]";

  variables.put("x0", x0);
  variables.put("y0", y0);
  variables.put("x1", x1);
  variables.put("y1", y1);
  variables.put("weight", 1 + random(4));
  variables.put("colour", randomColor());
  variables.put("label0", label0);
  variables.put("label1", label1);
}

int randomColor() {
  int r = 100 + int(random(156));
  int g = 100 + int(random(156));
  int b = 100 + int(random(156));
  return color(r, g, b);
}

```

---

<div class="post-metadata">

**Author:** ![Luka](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@Luka](https://discourse.processing.org/u/Luka)\
**Post date:** [September 22, 2018, 11:25am UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730/5 "2018-09-22T11:25:44Z")

</div>

@quark thank you for your answer!

It doesn’t really solve my idea to optimise the flow. I will probably use if sentences like before just to change from numbers to variable names. So instead of

```auto
  // /ctlin - Renoise-midi > SuperCollider-OSC
  if(theOscMessage.checkAddrPattern("/ctlin")==true) {
    // screenClean
    if(oscVal1.equals( "10" )) { screenCleanHue = oscVal2 ; }
    if(oscVal1.equals( "11" )) { screenCleanSaturation = oscVal2 ; }
    if(oscVal1.equals( "12" )) { screenCleanBrightness = oscVal2 ; }
    if(oscVal1.equals( "13" )) { screenCleanAlpha = oscVal2 ; }
    if(oscVal1.equals( "14" )) { screenCleanFlickrAmount = oscVal2 ; }
    if(oscVal1.equals( "15" )) { screenCleanFlickrSpeed = oscVal2 ; }
....

```

I will write:

```auto
    // screenClean
    if(oscVal1.equals( "screenCleanHue" )) { screenCleanHue = oscVal2 ; }
    if(oscVal1.equals( "screenCleanSaturation" )) { screenCleanSaturation = oscVal2 ; }
    if(oscVal1.equals( "screenCleanBrightness" )) { screenCleanBrightness = oscVal2 ; }
    if(oscVal1.equals( "screenCleanAlpha" )) { screenCleanAlpha = oscVal2 ; }
    if(oscVal1.equals( "screenCleanFlickrAmount" )) { screenCleanFlickrAmount = oscVal2 ; }
    if(oscVal1.equals( "screenCleanFlickrSpeed" )) { screenCleanFlickrSpeed = oscVal2 ;
....
 }

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 22, 2018, 11:46am UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730/6 "2018-09-22T11:46:34Z")

</div>

I am not convinced that your approach will be quicker than using a HashMap, especially if you have a large number of variables because the String equals method is slow whereas the HashMap does not perform string comparisons.

You can improve you approach by using else-if like this

```auto
// screenClean
    if(oscVal1.equals( "screenCleanHue" )) { screenCleanHue = oscVal2 ; }
    else if(oscVal1.equals( "screenCleanSaturation" )) { screenCleanSaturation = oscVal2 ; }
    else if(oscVal1.equals( "screenCleanBrightness" )) { screenCleanBrightness = oscVal2 ; }
    else if(oscVal1.equals( "screenCleanAlpha" )) { screenCleanAlpha = oscVal2 ; }
    else if(oscVal1.equals( "screenCleanFlickrAmount" )) { screenCleanFlickrAmount = oscVal2 ; }
    else if(oscVal1.equals( "screenCleanFlickrSpeed" )) { screenCleanFlickrSpeed = oscVal2 ;

```

if oscVal1 == “screenCleanHue” then in your code there will be 6 comparisons even though it matches the first one. Using else-if there is just 1 comparison the last 5 statements being ignored.

The problem is if oscVal1 == “screenCleanFlickrSpeed” then both codes need to perform 6 comparisons to find the match. This is where the HashMap is likely beat the multiple if and multiple else-if statements.

---

<div class="post-metadata">

**Author:** ![Luka](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@Luka](https://discourse.processing.org/u/Luka)\
**Post date:** [September 22, 2018, 3:11pm UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730/7 "2018-09-22T15:11:30Z")

</div>

Yes, in terms of speed this is another question and pretty good answer. If i’ll see things getting slow because of that I might definitely try the HashMap approach. However I’m wondering how does HashMap find the right key and how quickly? Probably there are optimised and memory efficient sorting algorithms there?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 22, 2018, 5:12pm UTC](https://discourse.processing.org/t/how-to-use-a-variable-name-thats-stored-inside-the-variable-reflection/3730/8 "2018-09-22T17:12:26Z")

</div>

In Java the key is passed through a hashing function to create a 32 bit integer. You can see this yourself assume you have created an object of some class (doesn’t matter which one) we can see the hashcode using `myobject.hashCode()`. A 32bit integer has over 4 billion possible values and the best hashing algorithms provide an even distribution over this range.

OK so we have the original “key-value pair” and the “key-hashcode” these are stored in a tree structure using the “key-hashcode” as the key and the “key-value pair” as the value.

So if we provide a key then the hashmap will generate the hashcode then search the tree\* and return with the key-value pair then simply returns the value part.

- searching a binary tree is exceptionally fast - O(log2 N) performance where log2 is the logarithm to base 2

If you know the range and maximum number of keys to be stored it is possible to design and implement a hashmap that works even faster - O(1) performance
