Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am trying to turn a nice bit of code into a space-saving sub-routine. Anyway, all it wants to do is compare two DNA strings at every position to see if they are correctly paired (where A-T and C-G are correct pairings). This code worked lovely as a non-subroutine and i think the problem lies with passing more than one array into the sub by reference. Hope someone can correct my stupidity ;->
my $mismatch = get_mismatches (\@dna, \@complement); sub get_mismatches { my ($dna, $complement) = @_; print "<P>TESTING TESTING TESTING SEGMENT::::: @$ +dna <P> COMP::::::: @$complement<P><P>"; my $i; my %good_pairs = ( 'A' => 'T', 'C' => 'G', 'G' => 'C', 'T' => 'A', ); foreach (my $i =0; $i < @$dna; $i++) { if ($dna[$i] ne $good_pairs {$complement[$i]}) { print "SUB - MISMATCH: $dna[$i-1]$dna[$i]/$complement[$i- +1]$complement[$i]\n"; push @seg_mismatches,"$dna[$i-1]$dna[$i]/$complement[$i-1 +]$complement[$i]\n"; print "SUB - MISMATCH: $dna[$i]$dna[$i+1]/$complement[$i] +$complement[$i+1]\n"; push @seg_mismatches, "$dna[$i]$dna[$i+1]/$complement[$i] +$complement[$i+1]\n"; push @seg_mis, "$dna[$i-1]$dna[$i] $dna[$i]$dna[$i+1]\n"; } } return (@seg_mismatches); }

20030325 Edit by Corion: Changed title

  • Comment on Problem while refactoring code into a subroutine (was: subroutines!!!)
  • Download Code

Replies are listed 'Best First'.
Re: subroutines!!!
by grantm (Parson) on Mar 25, 2003 at 11:26 UTC

    Instead of:

    $dna[$i]

    You want:

    $dna->[$i]

    Since $dna and $complement are array references, you need the arrow to de-reference back to the underlying array.

      If i wanted to return two arrays instead of one; e.g.
      return (\@array, \@array2);
      What is the syntax to using these arrays once they have been returned?? cheers

        You do exactly the same thing. The values you get back will be array references so you access the elements using the -> syntax.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: subroutines!!!
by davorg (Chancellor) on Mar 25, 2003 at 11:27 UTC

    Your problem is that you have array references not arrays. You therefore need to access the individual elements using, for example, $dna->[$i] and not $dna[$i].

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: subroutines!!!
by dragonchild (Archbishop) on Mar 25, 2003 at 13:57 UTC
    To expand further, this problem could've been found immediately. Add the following lines to the top of your program:
    use 5.6.0; use strict; use warnings;
    If that breaks saying that your perl is not 5.6.0 or higher, do the following:
    #!/your/perl/here -w use strict;
    You might have a lot of statements saying that "Such'n'such isn't declared". Use my liberally.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: subroutines!!!
by zby (Vicar) on Mar 25, 2003 at 11:29 UTC
    Just try $dna->[$i], and in other places in the same way. $dna is a reference - read a bit about references in Perl.