Loop Control

An example for using the next command

This program copies UniGene flat file such that sequence information is omitted.
#!/usr/bin/perl

$source_file = "unigene.txt";
$result_file = "unigene1.txt";

open (SOURCE, $source_file) || die "cannot open \"$source_file\": $!";
open (RESULT, ">$result_file") || die "cannot open \"$result_file\": $!";

#Use a special variable to change the input record separator
#to be "//\n" instead of \n
#NOTE: this does not remove the record separator;
#each multi-line $entry will end with "//\n"
$/ = "//\n";

ENTRY: while ($entry = <SOURCE>) { #reads until //\n inclusive

   @lines = split (/\n/, $entry);
   
   foreach $line (@lines) {
      
      if (substr ($line, 0, 8) eq 'SEQUENCE') {
         print RESULT "//\n";
         next ENTRY;
      }
      print RESULT "$line\n";
   }  
}

$/ = "\n";  # restore default record separator

Table of Contents.
Previous | Next.