in reply to Re: Arithmetical Simulation
in thread Arithmetical Simulation

I considered preprocessing, but that would have me involving in posing restriction to the flexibility of feeding any number larger than 9 to a certain array position (or so thought I). You have actually made justice to this situation, so thankful I am indeed, I think preprocessing should be considered, however, I wished your answer was more commented, I am still in the start of learning Perl the right way so some advanced things require someone opening my eyes for me and directing my attention there, there are some places in your reply where I could not understand the concept because some things I faced them first time, like this line
@$_ > $maxDigits and $maxDigits = @$_ for @numbers;
I mean the @$_ to be specific.....Thank you and ++ , I will of course read about it and learn it :)
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

Replies are listed 'Best First'.
Re^3: Arithmetical Simulation
by GrandFather (Saint) on Jul 31, 2009 at 21:28 UTC

    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
      let me be frank, I thought using (logical and) can be buggy, until when you said it works as an if, that is when I REMEMBERED (logical and) would actually short-circuit, I got my confusion compounded because I haven't reached to Perl references yet so I was wondering.In fact I was wondering on many things, it took you about 10 minutes to reply my first query and with such neat neat code and it took me 3 hours almost to work out a code of my own, so you know I was baffled as to when I could get better, but I am not ashamed of it, since I am still learning. I am motivated that I got to know Perl Monks, you guys are so helpful and caring too... As for the bug, I could not notice nothing cuz some of the concepts you employed, I am yet to reach them like using square brackets inside map{} as in
      my @numbers = map {[reverse split '']} qw(109156 114);
      and the other two lines, this is why I am strongly intent on coming back to this code every other while to measure my progress.
      Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind