2

I use Perl to analyze my research data (multiple large files which may be edited or modified by users while program is running).

In my program, there are scripts to check whether the file is complete or not before it analyze the data in one of files. This check is processed in multiple loops. If I simply use "exit", it only exit a loop. How can I force the scripts to quit and generate an error message for user before it quit? In my program, there is a defined variable which be output to a log file at the end of the program. I do NOT want to use GOTO command. Any further information is highly appreciated.

    ......
foreach $dir (@dirs)
{
   ...
   $file="$dir$filename";
   $file_size=`wc -l $file`;
   $line=`grep -n TIMESTEP $file`;

   #read the first line no of each frame in a data file
   @values=split(/\r?\n/,$line);
   $loop_i=0;
   $tmp=0;   #save line no for the first frame 
   foreach $sub_line (@values)
   {
     @sub_values=split(/:/,$sub_line);
     $line_no[$loop_i]=$sub_values[0];
     #check the line number in each frame same or not, if not quit
     if($loop_i==1){$tmp=$line_no[$loop_i]-$line_no[$loop_i-1];}
     elsif($loop_i>1)
     { $_=$line_no[$loop_i]-$line_no[$loop_i-1]; 
       if($_ <> $tmp) 
       {$flag=0; $err_message="$err_message; incomplete data (each frame has different line number)"; 
       exit;   #cannot quit the whole program 
       } 
     }
     else{;}
     $loop_i++;
   }#end foreach $sub_line (@values)
   .....
}#end foreach $dir (@dirs)

....
Leon
  • 444
  • 2
  • 15
  • 1
    [There is no such thing as PERL](https://perldoc.perl.org/perlfaq1.html#What's-the-difference-between-%22perl%22-and-%22Perl%22%3f) – Quentin Jan 07 '19 at 16:13
  • 7
    `exit` will, generally, quit the entire program. You need to provide a [mcve]. – Quentin Jan 07 '19 at 16:15
  • Er... Show this code where `exit` doesn't end the program? – Shawn Jan 07 '19 at 16:15
  • 1
    Check out https://stackoverflow.com/questions/3940106/how-can-i-redirect-output-of-die-function-to-a-file-in-perl – con Jan 07 '19 at 18:42
  • I think this is an interesting question although you need to add some specifics to help us answer it. Maybe we can re-open this when we know a bit more. – brian d foy Jan 08 '19 at 02:15
  • Thanks for your comments. Following your suggestion, I added code example in my question. If you have any questions, please let me know. Thanks again. – Leon Jan 08 '19 at 15:37
  • @con, thanks for your link. I tested it. It does not work on my case. I provided a code example in my questions. Any further help would be highly appreciated. – Leon Jan 08 '19 at 17:27
  • You're missing a closing `` ` `` in your `$line` line. Does this typo exist in your code? – divibisan Jan 08 '19 at 18:37
  • no. Sorry about that. I manually input the code because my original code consists of other settings. I do not want to show everything here. – Leon Jan 08 '19 at 19:46
  • @brian d foy, yes. If you could add command "quit" in PERL, it would be better. – Leon Jan 08 '19 at 20:52

1 Answers1

1

I think what you want are loop controls. You can use next, last, or redo to break out of a loop early, stop a loop completely, or process the same iteration again. With nested loops you can use a label to specify which loop you want to control:

DIR: foreach my $dir ( ... ) {
    ...
    LINE: foreach my $line ( ... ) {
        next LINE if $skip_line;
        last LINE if ...;
        next DIR if ...;
        }
    }
brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • Thanks Brian! Is there any other way to avoid the label. I do not want to "ruin" the program structure. Any further suggestion would be highly appreciated. – Leon Jan 10 '19 at 15:49
  • You aren't ruining the structure. This is how Perl intends you to do this. – brian d foy Jan 10 '19 at 17:34