Logo

Programming-Idioms

History of Idiom 195 > diff from v4 to v5

Edit summary for version 5 by daxim:
New Perl implementation by user [daxim]

Version 4

2019-09-29, 09:17:20

Version 5

2019-09-29, 11:22:48

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Imports
use List::Util qw(max);
Code
sub foo {
    my ($A) = @_;
    my $i_size = @$A;
    my $j_size = max map { 0 + @$_ } @$A;
    printf "dimensions: %d %d\n", $i_size, $j_size;

    my $s;
    for my $i (1 .. $i_size) {
        for my $j (1 .. $j_size) {
            $s += $A->[$i - 1][$j - 1] * $i * $j;
        }
    }
    printf "sum: %f\n", $s;
}