Adding is such fun!

Nov 27, 2007 12:30

At lunch time a subset of the people who write models of ARM chips here in Cambridge go to Tesco. Some to buy lunch, others just to have a walk. We meet in a Jabber chat room to indicate if we are going or not to try and cut down on cat herding. The basics are covered by t++ and t--, but being geeks we get bored of this simple and common construct, so we get some variants:
LET T=T+1

T:=succ(T);

S_TESCO <= CATSLIB.TESCO'succ(S_TESCO);

tesco.IncrementPopulationCounterThreadSafeWithPlusOneExceptOnMondaysAndThursdays()

So I had to come up with something better. The obvious choice being Perl. So, how does one add in Perl?

$t++

Not very exciting...

$t =~ tr/[0,1,2,3,4,5,6,7,8,9]/[1,2,3,4,5,6,7,8,9,10]/;

Well, there aren't many of us, so it works. You can also jumble the numbers around to confuse people. It turns out that, at least with the version of Perl I am using, you can embed code in your regexp:

$t =~ s/(\d+)(?{ $_ = $1+1 })/$_/;

Not bad. But it turns out you can be more sick:

$tesco =~ s/(\d+)(?{ sub add1 { return @_[0]+1 }; $_ = add1( $1 ) })/$_/;

I quite like that :-)

perl

Previous post Next post
Up