Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semna
    Member
    • Apr 2010
    • 55

    #1

    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
    Member
    • Apr 2010
    • 55

    #2
    moreover, curly braces are OK.

    Comment

    • kmcarr
      Senior Member
      • May 2008
      • 1181

      #3
      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.

      Comment

      • semna
        Member
        • Apr 2010
        • 55

        #4
        Hi KmCarr,
        I am a new user in perl. As I learn, return is needed for subroutine. So where should I do?
        Thanks

        Comment

        • krobison
          Senior Member
          • Nov 2007
          • 734

          #5
          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`;
              }
          }

          Comment

          • semna
            Member
            • Apr 2010
            • 55

            #6
            Thanks So much Krobison for your help.

            Comment

            Latest Articles

            Collapse

            • SEQadmin2
              Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
              by SEQadmin2



              Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

              Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
              ...
              10-14-2026, 09:55 AM
            • SEQadmin2
              New Genomics Technologies Take Aim at Long-Standing Limits
              by SEQadmin2


              Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

              We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
              ...
              09-28-2026, 10:25 AM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by SEQadmin2, Yesterday, 11:13 AM
            0 responses
            16 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, Yesterday, 10:48 AM
            0 responses
            17 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-15-2026, 10:52 AM
            0 responses
            35 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-14-2026, 05:39 AM
            0 responses
            42 views
            0 reactions
            Last Post SEQadmin2  
            Working...