I must admit that some of my intent was to make you look hard at the code and think and about how it works. I hope that the variable names convey the intent of the code even if the mechanism is not obvious for someone starting out.
$_ is of course the default variable. The line
my @numbers = map {[reverse split '']} qw(109156 114);
generates two entries in @numbers. Each entry is an array of the digits in the number with the order from least significant digit to most significant.
@$_ > $maxDigits and $maxDigits = @$_ for @numbers;
iterates over the numbers (which are stored as arrays of digits remember) and sets $_ to the reference to each digit array in turn. Thus @$_ is the array of digits in the current array which in scalar context is simply the number of digits.
The other trick there is using an and in place of an if to have the assignment only happen if the current number of digits is greater than $maxDigits. Use this particular trick sparingly. I only used it here to avoid using a full for loop rather than for as a statement modifier. Nested statement modifiers are not allowed.
For bonus points, can you see the bug in the code?
Did you notice that you can provide any number of numbers?
Would a short comment have conveyed that information? Would a long comment be useful when you have progressed to the point of recognising @$_? In general comments of that nature should be avoided - at best they obscure the actual code and at worst they may be wrong. In either case they double the effort of maintenance. It is much better to use good identifiers and code structure to convey the intent of code with comments used to provide information about interaction between different parts of the code and other 'global' information that can not be gleaned from the code itself.
True laziness is hard work
|