Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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;
    }
    }
    }

  • #2
    moreover, curly braces are OK.

    Comment


    • #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


      • #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


        • #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


          • #6
            Thanks So much Krobison for your help.

            Comment

            Latest Articles

            Collapse

            • seqadmin
              Current Approaches to Protein Sequencing
              by seqadmin


              Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
              04-04-2024, 04:25 PM
            • seqadmin
              Strategies for Sequencing Challenging Samples
              by seqadmin


              Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
              03-22-2024, 06:39 AM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by seqadmin, 04-11-2024, 12:08 PM
            0 responses
            30 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-10-2024, 10:19 PM
            0 responses
            32 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-10-2024, 09:21 AM
            0 responses
            28 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-04-2024, 09:00 AM
            0 responses
            53 views
            0 likes
            Last Post seqadmin  
            Working...
            X