storing a previously read Serial.read()

hey, does anyone know how to store a previously serial.read()? for example i want to run a loop where val=Serial.read() and then i want to say if the new serial.read = the previous serial.read do nothing. or if the new val is different from the previous val than i would want to find the difference... any help would be great...

Is this what you are looking for?

void loop() 
{ 
  char val, oldval;
  
  if ( Serial.available()) {
    val= Serial.read();
    if(val != oldval){
        char difference = oldval - val;
    }    
  }
}

Note that the value of difference will be meaningless the first time through because oldval has not been read yet.

If you are looking for something more complicated, have a dig around for the Atmel application note avr135, although primarily about PWM, the source code example uses a queue that can be implemented to store any number of values.