#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1

    Techniques for slicing Arrays


    Take a look at this code :

    Code:
    my $DNASTRING = "atgtttaaatagatgcatttttga";
    my @codons = ($DNASTRING =~ /.../g); 
    #break the string into codons store each codon as an element
    #of an array
    What I want to do is create another array, but this array I want to have it's elements a string starting with atg and ending with taa, tag or tga. Is there any way I can achieve this?

    In the array I created above, for example, the 0th element is atg, 1st ttt... 3rd tag. I want to slice those elements off the array and store it into another array. So on and so forth for all the elements of the @codons array. Is there a technique to do that?
    Last edited by seedofwinter; December 23rd, 2013 at 10:13 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Sure you can slice an array, but I did not understand the rules you want to apply.

    Assuming you want an array containing the 3 first elements of @codons, you can just do this:

    Perl Code:
    my @first_slice = @codons[0..2];
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1
    Originally Posted by Laurent_R
    Sure you can slice an array, but I did not understand the rules you want to apply.

    Assuming you want an array containing the 3 first elements of @codons, you can just do this:

    Perl Code:
    my @first_slice = @codons[0..2];
    I'm trying to investigate another avenue of solving my problem, I don't think it's possible to do what I want to be done. Let me try to expalin again:

    I have a string, let's say $DNA = "atgaaattttaaatggggtag";

    I want to split that line into segments of 3 and store each packet of 3 into an element of an array:

    @codons = ($DNA =~/.../g);

    Now, the 0th element of the array ill have atg, the 1st element..... the 4th element taa. What I want to do is split the codons array into another array, but this array will be each segment of the string that start with atg and ends with taa tga or tag.

    For example, the kind of operation I wish to be done will create an array of two elements out of this @codons array, one with attaaattttaa and the other element the other half of the string.

    Can this be done? IS there an operation on arrays that can let me take all the elements of the array up to the element of tag, put them all into an element of another array, and continue to do that until the the string is done?
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,259
    Rep Power
    1810
    Here's one method:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    use List::MoreUtils qw/first_index/;
    
    my $seq = 'atgtttaaatagatgcatttttga';
    
    # create a hash of the possible ending tags
    my @ends = qw/taa tga tag/;
    my %end;
    undef @end{@ends};
    
    # split the string into chunks of three
    my @codon = unpack '(A3)*', $seq;
    
    # find the location of the first match
    my $i = first_index { exists $end{$_} } @codon;
    print "$i: $codon[$i]\n";
    
    my @match = @codon[0..$i];
    my @remain = @codon[$i+1..$#codon];
    
    print Dumper \@match;
    print Dumper \@remain;

IMN logo majestic logo threadwatch logo seochat tools logo