Question

I am using a while loop to assign real numbers from an input file to an array. The input file is set up in a way that makes the first 21 points in the array blank. The number 21 corresponds to the line number containing the phrase "Range1" in my input file minus 1. I am using

splice @range_min, 0, $header_lines-1;

to remove those blank lines from my array.

I want $header_lines to be equal to the line number containing that phrase since I could potentially have other input files with that phrase on a different line.

I have already tried putting the following command into my while loop:

$header_lines = $., last if /Range1/;

which does work but it completely clears my array (defeating the purpose of my program).

Is there a better way to find the line number of my phrase in an input file without affecting my while loop or array?

Was it helpful?

Solution

Try putting this in your loop instead:

    if ( /Range1/ ) {
        $header_lines = $.;
    }

I think what's currently happening is that everytime through the loop, you are setting $header_lines to the current line number ($.). The code "last if /Range1/;" quits out of your loop when $_ matches Range1. So you would only be setting the first 21 lines, and then quitting, and then erasing all the lines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top