Hi there, I want to write a function that can return one record from a file ervery time, for example, the records in input.txt are like this:

_________________________ 12 dd DH: mm struct{ int a; char b; m1 struct { int c; int d; }; m2 struct { int e; int f; }; } 2 dfdkk df dd } 1 LOCAL_PARAM: ssi_struct { ref = 0; header = { req = 0xa; }; } DH: mm struct{ int a; char b; m1 struct { int c; int d; }; } ____________________________________

I want to use a function to extract the records starting with "DH:" each time I call it,like this $record = get_record("input.txt"); So I must remember the position of the file last read, and continue to extract the record. I use the seek and tell, but it didn't work. Here is the script:

sub get_record_from_file { my $filein = shift; my $regex = shift; my $pos = shift open FILEIN, "<$filein" or die "!$\n"; seek(FILEIN, $pos, 0); my $in_block = 0; my $last = "}"; my @array = {}; foreach my $line (<FILEIN>) { if ($line =~ /$regex/) { $in_block = 1; } if ($in_block) { push @array, $line; } if ($line =~ m/$last$/) { $in_block = 0; } } $pos = tell FILEIN; return (\@array, $pos); } $record = get_record_from_file("input.txt","DH:",0);

Can anyone help? Thanks:)

Update

correct some mistakes: Here is the script:

sub get_record_from_file { my $filein = shift; my $regex = shift; my $pos = shift open FILEIN, "<$filein" or die "!$\n"; seek(FILEIN, $pos, 0); my $in_block = 0; my $last = "}"; my @array = {}; foreach my $line (<FILEIN>) { if ($line =~ /$regex/) { $in_block = 1; } if ($in_block) { push @array, $line; } if ($line =~ m/$last$/) { $in_block = 0; last; } } $pos = tell FILEIN; return (\@array, $pos); } my $curpos = 0; ($record,$curpos) = get_record_from_file("input.txt","DH:",$curpos);

The $curpos is not updated. For example, you call the function once, and return the $curpos = 174; next time I call it, $curpos is still 174. Thanks:)


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.