Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to split an array identified by numbers to get the correct sequence of elements

by supriyoch_2008 (Scribe)
on Apr 19, 2012 at 11:20 UTC ( #965917=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I am a beginner in perl programming. I need a perl program to split an array and to get the sequence of elements identified by number from left to right. For example, I have an array

@k=”CCATGNNNTAACCNNATGNNTAGCC”;

I am interested in getting the substrings between elements 3 and 11 and between 16 and 23 (first position marked as 1).

#!usr/bin/perl-w @k=”CCATGNNNAACCNNATGNNTAGCC”; $s3-11=code?? $s16-23=code?? Code???? Print” \nThe sequences are: \n $s3-11\n\n $s16-23.\n\n”; exit;

Result should be like this:

ATGNNNTAA

ATGNNTAG

Comment on How to split an array identified by numbers to get the correct sequence of elements
Select or Download Code
Re: How to split an array identified by numbers to get the correct sequence of elements
by Anonymous Monk on Apr 19, 2012 at 11:54 UTC
      Note: In perl (unlike in "C"), strings are not arrays. Your array has only one element - the string. It is possible to split the string into array elements and process that array, but it is almost always better to store and process your data as a string. All other responders have used this approach without comment.
Re: How to split an array identified by numbers to get the correct sequence of elements
by JavaFan (Canon) on Apr 19, 2012 at 12:03 UTC
    #!/usr/bin/perl my $k = "CCATGNNNAACCNNATGNNTAGCC”; print substr($k, 2, 9), "\n"; print substr($k, 15, 8), "\n";
Re: How to split an array identified by numbers to get the correct sequence of elements
by trizen (Friar) on Apr 19, 2012 at 12:17 UTC
    my $string = "CCATGNNNTAACCNNATGNNTAGCC"; my @substrings; foreach my $offset ([3, 11], [16, 23]) { push @substrings, substr($string, $offset->[0] - 1, $offset->[1] - + $offset->[0] + 1); } my ($substr1, $substr2) = @substrings; print "$substr1\n$substr2\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965917]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-07-26 02:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls