Changing to Serial or Serial2.print depending on button state

Hi everyone, I'm struggling with the following situation. I would like to write a bit of code which changes from Serial to Serial2 depending on the the state of a button.

If Button = HIGH use Serial If Button = LOW use Serial2

i have quite a few strings to send but since they are the same for both the Serial and Serial2 connection i thought it would be quite nice to just change the Serial part of Serial.print instead of having two loops with the exact same messages but intended for different connections.

Here a bit of example code how i thought it could look like:

#define button 5

void setup(){
  Serial.begin(9600);
  Serial2.begin(9600);
  pinMode(button,INPUT);
}

void loop() {
  digitalRead(button);
  if(button == HIGH)
  {
    //change Serial output to Serial.println
    Serialprint();
  }
  else //button = LOW 
  {
    //change Serial output to Serial2.println
    Serialprint();
  }
}

void Serialprint()
{
  Serial or Serial2.println("TEST"};  // dependend on if button high or low 
}

Any ideas on this?

Thanks in Advance

Use a pointer or reference, then a simple assignment will direct output to the appropriate serial device.

Thanks for your response AWOL. Could you give me a short example how it would look like in my case?

typeof (Serial)& mySerial = Serial; // (global scope) A reference has to be initialised

if(button == HIGH)
  {
    mySerial = Serial;
  }
  else 
  {
    //change Serial output to Serial2.println
    mySerial = Serial2;
  }

AWOL: if(button == HIGH)  {    mySerial = Serial;  }  else  {    //change Serial output to Serial2.println    mySerial = Serial2;  }

Very neat

...R

Of course, since you read a value from the pin and threw it away, and then compared the pin NUMBER to HIGH, none of that code will do what you want.