Spooferman
Spooferman

Reputation: 353

How to repeatedly search the same file 'n' number of times using perl?

What I am actually trying to do is, extract range of lines from a text file and print the ranges in another file. The start range of strings are stored in @secarr and corresponding end range of strings are stored in @exarr. The problem now I am facing is that $secarr[4] and $exarr[4] range is found near the EOF and hence the output file ends till there. But $secarr[5] and $exarr[5] and many other ranges are present before $secarr[4] and $exarr[4] range.

Please suggest me a way out.

Thanks in advance, Faez

Upvotes: 0

Views: 111

Answers (2)

pavel
pavel

Reputation: 3508

You may want to have a look at Tie::File, which gives you a simple interface to the records of your file.

Upvotes: 0

choroba
choroba

Reputation: 241998

The simple approach is to reopen the file for each range. You can also try:

  • If the file is not huge, you can read it to an array (my @array = <$IN>;). For each range, you just print the array slice.
  • You can go over the file just once, checking whether you are entering/leaving any range on each line. Remember the currently active ranges and output each line to all of them (can your ranges overlap?)

Upvotes: 3

Related Questions