CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
splitting an array

 



onephattai
Novice

May 7, 2009, 10:22 PM

Post #1 of 8 (991 views)
splitting an array Can't Post

Hello,

I have an array that looks like this:
@codon = cagacctaggcctag
and I would like to split it into 3 variables, each variable with 3 letters. So variable 1 gets "cag", variable 2 gets "acc" and variable 3 gets "tag" and the left over is discarded. I'm thinking of doing it this way:
($v1, $v2, $v3) = splice(@codon, 0, 3);
but this didn't work. Please help!
thanks,


KevinR
Veteran


May 7, 2009, 10:41 PM

Post #2 of 8 (989 views)
Re: [onephattai] splitting an array [In reply to] Can't Post

You have an array with one element or many elements? Please provide a better example of your array.
-------------------------------------------------


onephattai
Novice

May 7, 2009, 11:10 PM

Post #3 of 8 (987 views)
Re: [KevinR] splitting an array [In reply to] Can't Post

The input file looks like this:
Woft Spider:cagacctaggcctag
House Fly:cagacctaggccatc

this is my script:
die "Usage: $0 Name\n" if @ARGV !=2;
open (INFILE, "$ARGV[0]") || die "Cannot open $ARGV[0] for reading.\n";
open (OUTFILE,">$ARGV[1]") || die "Cannot open $ARGV[1] for writting.\n";


foreach (<INFILE>)
{
($name, @codon) = split /:/;

($n1, $n2, $n3) = split /\.{3}/, @codon;

}

I'm able to extract the name and codon. If you do a print on $name and @codon, it would give you
$name = Woft Spider
@codon = cagacctaggcctag

The array codon has 1 element. I would like to split @codon into 3 variables and each variable contains 3 letters. So it would be $var1=cag, $var2=acc, $var3=tag. As you can see, the way I'm slitting @codon doesn't work.


(This post was edited by onephattai on May 7, 2009, 11:15 PM)


KevinR
Veteran


May 7, 2009, 11:33 PM

Post #4 of 8 (981 views)
Re: [onephattai] splitting an array [In reply to] Can't Post

It appears to me you are going about it a bit wrong. There is no need to split the lines into a scalar and an array. Split them into two scalars and then use the unpack() function to get the three sequences stored into three scalars:


Code
foreach (<INFILE>) {  
($name, $codon) = split /:/;
($n1, $n2, $n3) = unpack("A3A3A3", $codon);
print "$name $codon $n1 $n2 $n3\n";
}


Hopefully you are using "strict" and "warnings" in your perl programs:


Code
use strict; 
use warnings;


While they might seem like a pain in the arse at first, they will be your best friends in a very short time. Learn how to declare your variables properly within their intended scope of use.
-------------------------------------------------


onephattai
Novice

May 8, 2009, 11:09 AM

Post #5 of 8 (968 views)
Re: [KevinR] splitting an array [In reply to] Can't Post

Thanks for your help Kevin. I'm really having problem with this perl class.


KevinR
Veteran


May 8, 2009, 9:47 PM

Post #6 of 8 (954 views)
Re: [onephattai] splitting an array [In reply to] Can't Post


In Reply To
Thanks for your help Kevin. I'm really having problem with this perl class.


You're welcome. I can see that you are having problems with perl but as long as you show some effort and aren't just posting your programing requirements and expecting someone to write your code for you I am sure you will continue to get help.
-------------------------------------------------


onephattai
Novice

May 8, 2009, 9:51 PM

Post #7 of 8 (953 views)
Re: [KevinR] splitting an array [In reply to] Can't Post

Well my problem is the "teacher". I'm not compatible with his teaching style.Frown


vikas.deep
User

May 9, 2009, 2:08 AM

Post #8 of 8 (936 views)
Re: [onephattai] splitting an array [In reply to] Can't Post

(By your original query and the final one about your teacher) I guess you are or aspire to be a bionformatician. (I am not __)
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives