in reply to Arithmetical Simulation

There are several solutions to your problem. They are all different ways of preprocessing your data so that it is in a suitable state for the main algorithm. Ultimately they use the facility of split with an empty string to generate a list of single characters. You can either provide your input data as a number in string, or convert your array to a string (using join most likely), then use split to create a clean list of digits:

my @number = split '', '1234567';

However with a little more preprocessing the actual addition logic becomes much simpler. Consider:

use strict; use warnings; my @numbers = map {[reverse split '']} qw(109156 114); my $maxDigits = 0; @$_ > $maxDigits and $maxDigits = @$_ for @numbers; @numbers = map {[@$_, (0) x ($maxDigits - @$_)]} @numbers; my @result; my $carry; for my $digit (0 .. $maxDigits - 1) { my $sum = $carry; $sum += $_->[$digit] for @numbers; push @result, substr $sum, -1, 1, ''; $carry = $sum || 0; } @numbers = map {[reverse @$_]} @numbers; @result = reverse @result; print "@$_\n" for @numbers; print '-' x ($maxDigits * 2), "\n"; print "@result\n";

Prints:

1 0 9 1 5 6 0 0 0 1 1 4 ------------ 1 0 9 2 7 0

Suppressing the leading zeros is left as an exercise for the reader. ;)


True laziness is hard work

Replies are listed 'Best First'.
Re^2: Arithmetical Simulation
by biohisham (Priest) on Jul 31, 2009 at 13:49 UTC
    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

      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