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
              16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
              by SEQadmin2





              NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification,...
              10-30-2026, 06:46 AM
            • 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

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by SEQadmin2, Yesterday, 07:59 AM
            0 responses
            13 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 11-05-2026, 09:25 AM
            0 responses
            32 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 11-03-2026, 11:54 AM
            0 responses
            45 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-28-2026, 09:57 AM
            0 responses
            170 views
            0 reactions
            Last Post SEQadmin2  
            Working...