http://qs1969.pair.com?node_id=1176963


in reply to how to get average of matrices' elements?

The first time through your nested for loops, @m_avrg is uninitialized. You can uninitialized with:
my @m_avrg; for my $i ( 0 .. 2 ) { $m_avrg[$i] = [ map { $_ = 0 } 1 .. 3 ]; # $m_avrg[$i] = [ map { $_ => 0 } 1 .. 3 ]; # WRONG! }

UPDATE: I copy'n'pasted the wrong code originally. Yes, Anon's x operator is better.

See also: Basic debugging checklist

Use 4 single spaces for each level of indentation.

Replies are listed 'Best First'.
Re^2: how to get average of matrices' elements?
by Anonymous Monk on Nov 30, 2016 at 19:07 UTC
    That map is wrong, we're not building a hash. Just use $m_avrg[$i] = [ (0) x 3 ];
      That map is wrong, we're not building a hash.
      Please try running my code before you make such a claim. My code creates an array-of-arrays data structure with all elements initialized to 0.

      UPDATE: see my updated code.

        I did run your code. Here's the output from Data::Dump:
        [[1, 0, 2, 0, 3, 0], [1, 0, 2, 0, 3, 0], [1, 0, 2, 0, 3, 0]]
Re^2: how to get average of matrices' elements?
by Anonymous Monk on Nov 30, 2016 at 19:10 UTC
    Also, another way to avoid the "uninitialized variable" warning is to use the += operator.
    instead of this: $m_avrg[$a][$b] = ($m_avrg[$a][$b] + $list[$a][$b]); use this: $m_avrg[$a][$b] += $list[$a][$b];