Please don't reopen old threads in order to ask new questions. Start a new thread instead, and link back to the old one if necessary. Only post to old threads if you have something substantial to add to that conversation
Also, what you're asking is a trivial scripting problem and sounds very much like a homework question. The LQ rules
point out that we are not obliged to give you any direct help there.
But I will say that all it requires is a simple loop
where you save the value at the end of each iteration for comparison in the next step.