SEQanswers

Go Back   SEQanswers > Bioinformatics > Bioinformatics



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl semna Bioinformatics 6 09-16-2011 12:16 AM
perl? semna Bioinformatics 1 07-27-2011 07:05 AM
perl? semna Bioinformatics 8 07-18-2011 05:24 AM
concatenation by perl semna Bioinformatics 4 02-24-2011 10:49 AM
Perl Help Wei-HD Bioinformatics 9 02-25-2010 12:35 PM

Reply
 
Thread Tools
Old 08-17-2011, 01:44 AM   #1
semna
Member
 
Location: holland

Join Date: Apr 2010
Posts: 55
Default perl?

Hi
I am writing a script in perl. My question is about of part of this script. I have a subroutine that makes a problem. Everything is OK when I don't use SUB but when I use it just give me the first part of the file (just $i=0). but it seems should work as a loop. Any command appreciated.
sub edit
{
my ($list) = @_;
my @species = "";
my $pid = "";
my @tree = "";
@species = split "//", $list;
while(my $i = 0; $i < scalar @species; $i++) {
if($species[$i] =~ /RANK *: subspecies/ && $species[$i] =~ /PARENT ID * : (\d+)/){
$pid = $1;
# # print "$pid\n";
$species[$i] = `getz "[taxonomy:$pid]" -e`;
@tree = $species[$i];
return @tree;
}
}
}
semna is offline   Reply With Quote
Old 08-17-2011, 01:47 AM   #2
semna
Member
 
Location: holland

Join Date: Apr 2010
Posts: 55
Default

moreover, curly braces are OK.
semna is offline   Reply With Quote
Old 08-17-2011, 04:58 AM   #3
kmcarr
Senior Member
 
Location: USA, Midwest

Join Date: May 2008
Posts: 1,177
Default

Semma,

You exit the subroutine with the "return @tree;". As soon as it reaches that point during the first pass ($i=0) program flow returns to the calling point without processing the remainder of your @species list.
kmcarr is offline   Reply With Quote
Old 08-17-2011, 07:59 AM   #4
semna
Member
 
Location: holland

Join Date: Apr 2010
Posts: 55
Default

Hi KmCarr,
I am a new user in perl. As I learn, return is needed for subroutine. So where should I do?
Thanks
semna is offline   Reply With Quote
Old 08-17-2011, 09:08 AM   #5
krobison
Senior Member
 
Location: Boston area

Join Date: Nov 2007
Posts: 747
Default

In general, you don't want to return from withing

Also, when posting code here go to Advanced and use the CODE tags (# icon), as this makes examples much easier to read
Code:
sub edit
{
  my ($list) = @_;
  my @species = "";
  my $pid = "";
  my @tree = "";
  @species = split "//", $list;
  while(my $i = 0; $i < scalar @species; $i++) 
  {
    if($species[$i] =~ /RANK *: subspecies/ && $species[$i] =~ /PARENT ID * :    (\d+)/)
    {
       $pid = $1;
       # # print "$pid\n";
       $species[$i] = `getz "[taxonomy:$pid]" -e`;
       @tree = $species[$i];
       return @tree;
    }
  }
}
What you do depends on what you want. One solution is to return a much more sophisticated data structure which has all of the results in it (for example, a hash keyed by species with a reference to the tree list in the value)

Alternatively, do the split outside the subroutine and run the subroutine once on each value (I'm guilty of also editing your code to my style to be more concise). Also, you are always better using a foreach loop than explicit iteration (harder to make errors, more efficient, clearer in intent)

Code:
  my @species = split "//", $list;
foreach my $species(@species);
  {
    my @tree=&edit($species);
    ## do something here
}

sub edit
{
     my ($species) = @_;
    if($species =~ /RANK *: subspecies/ && $species =~ /PARENT ID * :    (\d+)/)
    {
       my $pid = $1;
       # # print "$pid\n";
       return `getz "[taxonomy:$pid]" -e`;
    }
}
krobison is offline   Reply With Quote
Old 08-18-2011, 12:38 AM   #6
semna
Member
 
Location: holland

Join Date: Apr 2010
Posts: 55
Default

Thanks So much Krobison for your help.
semna is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off




All times are GMT -8. The time now is 06:57 AM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Single Sign On provided by vBSSO