0

I would like to use perl to extract 30 character substrings from an array, using another array that consists of indices where I would like the substring from each line to start. For instance, Line 1 extract 30 characters starting at position 21, line 2 starting at 5, line 3 ... etc. Is this possible? I know its easy to do with a fixed index but I have never tried using indices stored in a separate array. Thanks so much.

For example, Array 1 would consist of strings for instance in my case:

ATGTCAAATCCAGAAAGCTTGAAAAAACAGGTTGAACCTCCTGGTTACAATGAGTTATTTATGGTGGAAGATGTTTGTAATGTGGACCTAGAGCAGGGACT
TGATTTGTGTAAGCCTGAAAAGGTAAACAAACAATCTCAACGATCTCGACAATCCCGACAATCCCTCTTTACCAACACCATTAAGCCTCAAAAAGACAAGA
TGAATATTAAAACAAATAAAATAAAAGAGTTTTTAAATGACCTTTTTACTGAATTTTCTAAATTCCACAATAGCTATTATCCTAATGGAAGAATTTCTACT
CAGGACAAATCTCGATGGGTCTTGCTTATTATTTGGTCTATTATCACTATTTTAACAATAGACAAGAAATTTAAAATAAAAGAGTCATATTTAGAATGGAT
AGGTGAAAATCAGTCCCACAGTGAAATTTGGGGGCCTATTGTTATTTATGTTGGCTTATTCATACTCTTATTGTCTGCTTTTAACTGTACGTTTCCTTCAC

And Array two consists of indices that I want to start each 30 char substring I extract from such as

21
32
15
7
17

Leaving an output of:

AAAAAACAGGTTGAACCTCCTGGTTACAAT
AATCTCAACGATCTCGACAATCCCGACAAT
ATAAAATAAAAGAGTTTTTAAATGACCTTT
AATCTCGATGGGTCTTGCTTATTATTTGGT
CAGTGAAATTTGGGGGCCTATTGTTATTTA
1
  • 1
    I'm not quite sure I understand you. Could you perhaps show code that you tried but didn't work as expected? Actual example data (instead of a description of this data) plus the expected output would help as well. Commented Jul 12, 2013 at 20:45

1 Answer 1

2

Lets assume we have a string, and an offset:

my $string = "ATGTCAAATCCAGAAAGCTTGAAAAAACAGGTTGAACCTCCTGGTTACAATGAGTTATTTATGGTGGAAGATGTTTGTAATGTGGACCTAGAGCAGGGACT";
my $offset = 21;
my $length = 30;

We can then get the required substring like

substr $string, $offset, $length;

But if we have an array @strings and a corresponding array @offsets, then for the i-th element we have:

substr $strings[$i], $offsets[$i], $length;

When we loop over all indices of the arrays (0 .. $#strings), we can get each substring.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.