tags:

views:

107

answers:

3

I'm building a count matrix in Perl using AoA: my @aoa = () then call $aoa[$i][$j]++ whenever I need to increment a specific cell. Since some cells are not incremented at all, they are left undef (these are equivalent to 0 counts).

I would like to print some lines from the matrix, but I get errors for undef cells (which I would simply like to print as zeros). what should I do?

+6  A: 

Use defined with a conditional operator (?:).

#!/usr/bin/perl

use strict;
use warnings;

my @matrix;

for my $i (0 .. 3) {
    for my $j (0 .. 3) {
        if (rand > .5) {
            $matrix[$i][$j]++;
        }
    }
}

for my $aref (@matrix) {
    print join(", ", map { defined() ? $_ : 0 } @{$aref}[0 .. 3]), "\n"
}

If you are using Perl 5.10 or later, you can use the defined-or operator (//).

#!/usr/bin/perl

use 5.012;
use warnings;

my @matrix;

for my $i (0 .. 3) {
    for my $j (0 .. 3) {
        if (rand > .5) {
            $matrix[$i][$j]++;
        }
    }
}

for my $aref (@matrix) {
    print join(", ", map { $_ // 0 } @{$aref}[0 .. 3]), "\n"
}
Chas. Owens
Why in the world is this getting a down vote?
Chas. Owens
+1 I have no idea, you're always helpful. thanks!
David B
+7  A: 

Classically:

print defined $aoa[$i][$j] ? $aoa[$i][$j] : 0;

Modern Perl (5.10 or later):

print $aoa[$i][$j] // 0;

That is a lot more succinct and Perlish, it has to be said.

Alternatively, run through the matrix before printing, replacing undef with 0.


use strict;
use warnings;

my @aoa = ();

$aoa[1][1] = 1;
$aoa[0][2] = 1;
$aoa[2][1] = 1;

for my $i (0..2)
{
    print join ",", map { $_ // 0 } @{$aoa[$i]}[0..2], "\n";
}
Jonathan Leffler
+1 thanks. I would like to get a row (`$aoa[$i]`) with zeros instead of `undef`s and then use `join ("'", @row)` to print with commas. How can I do that?
David B
@David B use a `map` instead of the inner `for`.
Chas. Owens
A: 

Just an example, please modify the code to your requirements.

use strict;
use warnings;

my @aoa;

$aoa[1][3]++;

foreach my $i (1 .. 3){
    foreach my $j (1 .. 3){
        defined $aoa[$i][$j] ? print $aoa[$i][$j] : print "0";
        print "\t";
    }
    print "\n";
}
Ibrahim
You aren't replacing `undef` with `0`.
Chas. Owens
As mentioned above my code that it is a sample, however I will fix the code to make it more acceptable.
Ibrahim
@Ibrahim That does work, but there is no reason to use the conditional operator if you are going to write it like that; just use an `if/else`. The beauty of the conditional operator is that it lets you factor out the common code like this `print defined $aoa[$i][$j] ? $aoa[$i][$j] : 0;`.
Chas. Owens