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

In reply to Re^3: Arithmetical Simulation by GrandFather
in thread Arithmetical Simulation by biohisham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.