Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by Jenda (Abbot)
on Apr 18, 2013 at 15:51 UTC ( #1029381=note: print w/replies, xml ) Need Help??


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

You either want to construct an iterator:

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;
or create a class.

Update: Fixed incorrect filehandle in the while loop. Thanks to poj.

Jenda
Enoch was right!
Enjoy the last years of Rome.

  • Comment on Re: I want to write a function, you call it ,it will return one matched record each time!
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029381]
help
Chatterbox?
[Discipulus]: figuring.. i'm still just use the side pm chat..
[erix]: (all this while karls guitar gently weeps...)
[planetscape]: (invade Hungary)++ #I'm hungry
[stonecolddevin]: i went too far down the irc rabbithole to hook this back up there
[Discipulus]: .. yes erix until NodeReaper put jhon coltrane again
[erix]: nice cover with the spitting image of George (and a great solo by Prince at the end )
[Discipulus]: good night monks! i feel fresh as the turkey in the hoven but i need to sleep

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2017-06-22 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (531 votes). Check out past polls.