in reply to how to set a value for a hash reference

The easiest thing is not to set the default, but apply the default while reading the value in the end, like my $ov = $overlap->{$a}{$b} // 0 (requires perl 5.10 or newer, but older versions than 5.12 aren't maintained anyway).

By the way a nicer way to write the inner loops is

my $last = @members - 1; for my $i (0 .. $last) { for my $j ($i + i .. $last) { $overlap->{$members[$i]}{$members[$j]}++; $overlap->{$members[$j]}{$members[$i]}++; } }

Replies are listed 'Best First'.
Re^2: how to set a value for a hash reference
by AWallBuilder (Beadle) on Mar 14, 2012 at 13:17 UTC

    thank you for the details and the loop suggestion. The "//" means if for a and b overlap doesn't exist then it equals zero, right?

    But where would I put this line? after the loop? no -> between the $a and $b? would I have to embed it in a loop with a and b being all elements of members?

      The "//" means if for a and b overlap doesn't exist then it equals zero, right?

      Correct. It's the "defined-or" operator.

      But where would I put this line? after the loop?

      Wherever your read the values of $overlap.

        hmmm - I guess its a bit complicated because I"m just passing the entire hash reference to another script that writes the output in a nice matrix format. And I don't want to modify "write_R_matirx". any other suggestions?

        use Smash::Utils::MatrixIO qw(:all); my $SMASH_overlap_file="$CDHIT_file.smash.overlap"; write_R_matrix($SMASH_overlap_file,$overlap);