Dear all,

I am a newbie to perl. I want to read file line by line in two sets i.e., odds and even. Means it should first read first line and second line then check for regular expression in second line with condition to print or not then move forward for 3 and 4 line then to 5 and 6 line and so forth.

the file contains millions of line so array is taking too much time and memory. I am a very newbie to perl so unable to find solution for this.

Any help will be appreciated.

Regards,
Saha

My question is: when you read the second line, is the regex match going to print out the contents of the first line? If so, this exercise is decidedly easy...

open(FILE, "<file.txt");
my $x=1;
my $last_line="";
while(<FILE>){
  if($x/2==int($x/2)){ #see if it's even
      if(/[regex]/){
           print $last_line;
      }
  }
   $x++;
   $last_line=$_;
}
close FILE;

Or am I missing something about the exercise? Do you need to store odd "last lines" or just work with the current one?


Dear all,

I am a newbie to perl. I want to read file line by line in two sets i.e., odds and even. Means it should first read first line and second line then check for regular expression in second line with condition to print or not then move forward for 3 and 4 line then to 5 and 6 line and so forth.

the file contains millions of line so array is taking too much time and memory. I am a very newbie to perl so unable to find solution for this.

Any help will be appreciated.

Regards,
Saha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.