How do I change this to “idiomatic” Perl?

前端 未结 6 2235
名媛妹妹
名媛妹妹 2021-02-09 03:46

I am beginning to delve deeper into Perl, but am having trouble writing \"Perl-ly\" code instead of writing C in Perl. How can I change the following code to use more Perl idiom

6条回答
  •  野性不改
    2021-02-09 04:32

    Instead of dereferencing your two-dimensional arrays like this:

    $$path_matrix[0][0] = 2;
    

    do this:

    $path_matrix->[0][0] = 2;
    

    Also, you're doing a lot of if/then/else statements to match against particular subsequences: this could be better written as given statements (perl5.10's equivalent of C's switch). Read about it at perldoc perlsyn:

    given ($matrix->[$row][$column])
    {
        when ($seq1_gap)       { $path_matrix->[$row][$column] = -1; }
        when ($match_mismatch) { $path_matrix->[$row][$column] = 0; }
        when ($seq2_gap)       { $path_matrix->[$row][$column] = 1; }
    }
    

提交回复
热议问题