You either want to construct an iterator:
or create a class.sub open_record_file { my $filein = shift; my $regex = shift; open my $FH, "<$filein" or die "$!\n"; return sub { my $in_block = 0; my $last = "}"; my @array; while (my $line = <$FH>) { if ($line =~ /$regex/) { $in_block = 1; } if ($in_block) { push @array, $line; } if ($line =~ m/$last$/) { $in_block = 0; last; } } return (@array ? \@array : undef); } } my $next_record = open_record_file("input.txt",'DH:'); while (my $record = $next_record->()) { # do something with the array in $record } undef $next_record;
Update: Fixed incorrect filehandle in the while loop. Thanks to poj.
Jenda
Enoch was right!
Enjoy the last years of Rome.
In reply to Re: I want to write a function, you call it ,it will return one matched record each time!
by Jenda
in thread I want to write a function, you call it ,it will return one matched record each time!
by dwslovedh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |