#! usr/bin/perl use strict; use warnings; use diagnostics; # 1. cumulative count of entries, as matrix is scanned columnwise. # Yielding: Ap = [ 0, 2, 5, 9, 10, 12 ]; # 2. row indices of entries, as matrix is scanned columnwise. # Yielding: Ai = [0, 1, 0, 2, 4, 1, 2, 3, 4, 2, 1, 4 ]; # 3. Non-zero matrix entries, as matrix is scanned columnwise. # Yielding: Ax = [2, 3, 3, -1, 4, 4, -3, 1, 2, 2, 6, 1]; my (@Ai, @Ax, @Ap,) = (); while () { chomp; my @elements = split /\s+/; ## process row-wise for (0 .. $#elements){ ## store non-zero elements by column if ($elements[$_] != 0){ ## store values by column push @{ $Ax[$_] }, $elements[$_]; ## store column index (minus one to make it start at zero) push @{ $Ai[$_] }, ($. -1); ## increment column totals ++$Ap[$_]; } } } ## make cumulative totals unshift @Ap, 0; for (1 .. $#Ap){ $Ap[$_] +=$Ap[$_-1]; } ## join array refs together @Ai = map{ @$_ } @Ai; @Ax = map{ @$_ } @Ax; print('@Ax = [', join(" ", @Ax), "]\n"); print('@Ai = [', join(" ", @Ai), "]\n"); print('@Ap = [', join(" ", @Ap), "]\n"); __DATA__ 2 3 0 0 0 3 0 4 0 6 0 -1 -3 2 0 0 0 1 0 0 0 4 2 0 1