This is exactly what I was going to suggest, apart from a minor optimisation regarding the initialisation of the first value. The I recalled of the existence of reduce
, which does the job nicely. But maybe I should have been less cryptic and explain what the solution does exactly - thanks for the example, I'll update my post linking to it :)
I know it's a corner case that should not happen, but I'd check for $i2 to be defined before comparing it against $id2.
Don't fool yourself.
perl -ple'$_=reverse' <<<ti.xittelop@oivalf