http://www.perlmonks.org?node_id=1029387


in reply to I want to write a function, you call it ,it will return one matched record each time!

6 more to correct..
# 1) add use strict; sub get_record_from_file { my $filein = shift; my $regex = shift; # 2) add ; at end my $pos = shift; open FILEIN, "<$filein" or die "!$\n"; seek(FILEIN, $pos, 0); my $in_block = 0; my $last = "}"; # 3) change {} to () my @array = (); # 4) change from foreach to while while (my $line=<FILEIN>) { if ($line =~ /$regex/) { $in_block = 1; } if ($in_block) { push @array, $line; } # 5) add $in_block if (($in_block) && ($line =~ m/$last$/)) { $in_block = 0; last; } } $pos = tell FILEIN; return (\@array, $pos); } my $curpos = 0; # 6) add my my ($record,$curpos) = get_record_from_file("input.txt","DH:",$curpos) +; # show results print for @$record; print "----\n"; ($record,$curpos) = get_record_from_file("input.txt","DH:",$curpos); print for @$record;
poj