I think you *intended* to burn off the lines as Scott7477 said; you are just missing the code to read the lines. Try this:
if ( /1tally\s\s(\d+)\s+nps\s=/i ) {
foreach ( 1 .. 5 ) {
my $junk = <OUT>;
}
next;
}
Similarly, inside the "
for (my $ij = 0; $ij < 98; $ij++) {...}" loop, you do not read any lines into
$_, so you reprocess the same line 98 times. Add the line "
$_ = <OUT>;" inside the loop, or use this next technique to keep from having to know the number of data lines ahead of time.
Another technique: When I want to skip the first part of a file, and the *last* line to be skipped can be matched by a regex, my preferred idiom is to use a skipping while-loop followed by a processing while-loop. You can also exit the processing while-loop if the file has a footer you need to skip. For example:
while (<OUT>) {
last if /^\s+energy\n$/;
}
while (<OUT>) {
last if /^\s+total\s+\d/;
/\s\s\s(\d+)\.(\d+)(\D+)(\d+)\s\s\s(\d+)\.(\d+)(\D+)(\d+)\s(\d+)\.(\
+d+)/
or warn "This line did not match the pattern: '$_'"
and next;
$energy_value = $1.$dot.$2.$3.$4;
#...
}
Other problems in your code:
- Your first line reads #!usr/bin/perl. There should be a slash between the exclamation mark and 'usr'.
- You don't have to use the filehandle OUT just because your filename is in a variable named $out_file. Change it to something like IN, or you will confuse your future maintenance programmers.
- All your tests to see if $_ is true, such as next unless $_; will *always* return true due to the trailing newline. Try this instead: next unless /\S/;
- In the pattern /\s\s\s(\d+).(\d+)(\D+)(\d+)\s\s\s(\d+).(\d+)(\D+)(\d+)\s(\d+).(\d+)/i, you do not need the "ignore-case" modifier, but you do need to back-whack your periods, and you probably should anchor the pattern at the beginning and the end. Corrected: /^\s*\s\s\s(\d+)\.(\d+)(\D+)(\d+)\s\s\s(\d+)\.(\d+)(\D+)(\d+)\s(\d+)\.(\d+)\s*$/
- That pattern, and the next three lines, could be simplified to this: my ($energy_value, $detector_value, $std_deviation) = /^\s*\s\s\s(\d+\.\d+E[-+]\d+)\s\s\s(\d+\.\d+E[-+]\d+)\s(\d+\.\d+)\s*$/;
- You have no way to distinguish between a line you should skip, and a line you should have processed but failed to due to some possible problem with your pattern; such lines will be *silently* *omitted*, and incorrect output will appear to be correct with no indication of the problem. See "another technique" above for a method that offers tighter control.
- You are not using strict or warnings. You will come to regret this!