Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by poj (Curate)
on Apr 18, 2013 at 17:09 UTC ( #1029387=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!

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


Comment on Re: I want to write a function, you call it ,it will return one matched record each time!
Download Code
Re^2: I want to write a function, you call it ,it will return one matched record each time!
by dwslovedh (Novice) on Apr 18, 2013 at 23:05 UTC

    Sorry for the wrong action. I known it now:) And really thank you for all the help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029387]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-08-01 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls