How to write this piece of code shorter? [SOLVED]

Does anyone have an idea how to write this peace of code in a shorter way, like

for (int i= 1; i < 13; i++){
}
while(finder.findUntil("DT", "\n\r")){
                int val = 0;
                val = finder.getValue(); 
                if (val == 1){
                  ip[0] = finder.getValue();
                }
                if (val == 2){
                  ip[1] = finder.getValue();
                }
                if (val == 3){
                  ip[2] = finder.getValue();
                }
                if (val == 4){
                  ip[3] = finder.getValue();
                }
                if (val == 5){
                  subnet[0] = finder.getValue();
                }
                if (val == 6){
                  subnet[1] = finder.getValue();
                }
                if (val == 7){
                  subnet[2] = finder.getValue();
                }
                if (val == 8){
                  subnet[3] = finder.getValue();
                }
                if (val == 9){
                  gateway[0] = finder.getValue();
                }
                if (val == 10){
                  gateway[1] = finder.getValue();
                }
                if (val == 11){
                  gateway[2] = finder.getValue();
                }
                if (val == 12){
                  gateway[3] = finder.getValue();
                }
              }
int val = finder.getValue();

would be a start

if (val == 1){
                  ip[0] = finder.getValue();
                }
                if (val == 2){
                  ip[1] = finder.getValue();
                }
                if (val == 3){
                  ip[2] = finder.getValue();
                }
                if (val == 4){
                  ip[3] = finder.getValue();
                }

I'm seeing a pattern there

int val = finder.getValue();

would be a start

hèhè good one, but no, not what had in mind.

This piece of code is used for getting data out a form posted on the web page this sketch serves.

so while the code is looking for "DT" is gives a value every time it finds DT in the URL. like this:

http://192.168.1.1/?ST=1&DT1=192&DT2=168&DT3=102&DT4=85&DT5=255&DT6=255&DT7=255&DT8=0&DT9=192&DT10=168&DT11=102&DT12=2

you can see DT runs from 1 till 12

/* I do not know if the second call to finder.getValue() is on
 * purpose, i.e. if it will return a value different from the first
 * call. If it does not, replace it with `val` */

while(finder.findUntil("DT", "\n\r")){
  int val = finder.getValue();

  if(val >= 1 && val <= 4) {
    ip[val - 1] = finder.getValue();
  }

  if(val >= 5 && val <= 8) {
    subnet[val - 5] = finder.getValue();
  }

  if(val >= 9 && val <= 12) {
    gateway[val - 9] = finder.getValue();
  }
}

Edit: probably should use else if to save some cycles.

Yup, that was the pattern

/* I do not know if the second call to finder.getValue() is on

  • purpose, i.e. if it will return a value different from the first
  • call. If it does not, replace it with val */

yes it is on purpose because the first one gives a different DT => DT1 then DT2 then DT3
and the second call finds the value behind the =

so for DT1=192 first finder.getValue() gives 1 and the second gives 192

@dominikh

it works, thanks. solved.

Even shorter

while(finder.findUntil("DT", "\n\r")){
    int val = finder.getValue(); 
    ip[(val-1)%4] = finder.getValue();
}

EDIT: This assumes that the first read of val can only return values 1-12. If that is not a good assumption, then adding an if(val >= 1 && val <= 12) would be needed.

EDIT2: oops, ignore this post

Even shorter

"subnet" and "gateway" ?

oops. did not see that. :blush: