Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 404991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:21:32+00:00 2026-05-12T17:21:32+00:00

I am beginning to delve deeper into Perl, but am having trouble writing Perl-ly

  • 0

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 idioms, and how should I go about learning the idioms?

Just an explanation of what it is doing: This routine is part of a module that aligns DNA or amino acid sequences(using Needelman-Wunch if you care about such things). It creates two 2d arrays, one to store a score for each position in the two sequences, and one to keep track of the path so the highest-scoring alignment can be recreated later. It works fine, but I know I am not doing things very concisely and clearly.

edit: This was for an assignment. I completed it, but want to clean up my code a bit. The details on implementing the algorithm can be found on the class website if any of you are interested.

sub create_matrix {
    my $self = shift;
    #empty array reference
    my $matrix = $self->{score_matrix};
    #empty array ref
    my $path_matrix = $self->{path_matrix};
    #$seq1 and $seq2 are strings set previously
    my $num_of_rows = length($self->{seq1}) + 1;
    my $num_of_columns = length($self->{seq2}) + 1;

    #create the 2d array of scores
    for (my $i = 0; $i < $num_of_rows; $i++) {
        push(@$matrix, []);
        push(@$path_matrix, []);
        $$matrix[$i][0] = $i * $self->{gap_cost};
        $$path_matrix[$i][0] = 1;
    }

    #fill out the first row
    for (my $i = 0; $i < $num_of_columns; $i++) {
        $$matrix[0][$i] = $i * $self->{gap_cost};
        $$path_matrix[0][$i] = -1;
    }
    #flag to signal end of traceback
    $$path_matrix[0][0] = 2;
    #double for loop to fill out each row
    for (my $row = 1; $row < $num_of_rows; $row++) {
        for (my $column = 1; $column < $num_of_columns; $column++) {
            my $seq1_gap = $$matrix[$row-1][$column] + $self->{gap_cost};
            my $seq2_gap = $$matrix[$row][$column-1] + $self->{gap_cost};
            my $match_mismatch = $$matrix[$row-1][$column-1] + $self->get_match_score(substr($self->{seq1}, $row-1, 1), substr($self->{seq2}, $column-1, 1));
            $$matrix[$row][$column] = max($seq1_gap, $seq2_gap, $match_mismatch);

            #set the path matrix
            #if it was a gap in seq1, -1, if was a (mis)match 0 if was a gap in seq2 1
            if ($$matrix[$row][$column] == $seq1_gap) {
                $$path_matrix[$row][$column] = -1;
            }
            elsif ($$matrix[$row][$column] == $match_mismatch) {
                $$path_matrix[$row][$column] = 0;
            }
            elsif ($$matrix[$row][$column] == $seq2_gap) {
                $$path_matrix[$row][$column] = 1;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:21:32+00:00Added an answer on May 12, 2026 at 5:21 pm

    I have some other comments as well, but here is the first observation:

    my $num_of_rows = length($self->{seq1}) + 1;
    my $num_of_columns = length($self->{seq2}) + 1;
    

    So $self->{seq1} and $self->{seq2} are strings and you keep accessing individual elements using substr. I would prefer to store them as arrays of characters:

    $self->{seq1} = [ split //, $seq1 ];
    

    Here is how I would have written it:

    sub create_matrix {
        my $self = shift;
    
        my $matrix      = $self->{score_matrix};
        my $path_matrix = $self->{path_matrix};
    
        my $rows = @{ $self->{seq1} };
        my $cols = @{ $self->{seq2} };
    
        for my $row (0 .. $rows) {
            $matrix->[$row]->[0] =  $row * $self->{gap_cost};
            $path_matrix->[$row]->[0] = 1;
        }
    
        my $gap_cost = $self->{gap_cost};
    
        $matrix->[0] = [ map { $_ * $gap_cost } 0 .. $cols ];
        $path_matrix->[0] = [ (-1) x ($cols + 1) ];
    
        $path_matrix->[0]->[0] = 2;
    
        for my $row (1 .. $rows) {
            for my $col (1 .. $cols) {
                my $gap1 = $matrix->[$row - 1]->[$col] + $gap_cost;
                my $gap2 = $matrix->[$row]->[$col - 1] + $gap_cost;
                my $match_mismatch =
                    $matrix->[$row - 1]->[$col - 1] +
                    $self->get_match_score(
                        $self->{seq1}->[$row - 1],
                        $self->{seq2}->[$col - 1]
                    );
    
                my $max = $matrix->[$row]->[$col] =
                    max($gap1, $gap2, $match_mismatch);
    
                $path_matrix->[$row]->[$col] = $max == $gap1
                        ? -1
                        : $max == $gap2
                        ? 1
                        : 0;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginning to use perl RegExp.I am using the following RegExp to catch unwanted blank
Beginning with Rails 3.1, class_inheritable_accessor produces deprecation warnings, telling me to use class_attribute instead.
Passing beginning date and final date as parameter through the following code as no
Since beginning to use VB.NET some years ago I have become slowly familiar with
I am beginning Facebook App development and am following the Recipe Box App Tutorial
This is the beginning of a script that displays thumbnails, worked fine, but I've
I am beginning WPF, and I am having a bit of a hard time
Beginning to play around with the Twitter API and chose to use the Twitterizer
Many beginning programmers write code like this: sub copy_file ($$) { my $from =
I am just beginning with sass and compass and stuff and writing my own

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.