In the TIMTOWDI spirit, here is one that uses some sugar cooked up by thedamian.

Be forewarned, it makes some assumtions about your file format that may not be true.
#!/usr/bin/perl -w use strict; $|++; ## # NOTE: This code Assumes (and we all know what that means) that the +file # being fed to has no more than one "boundary" (/FH|BH/) per line, and + # that the file is delimited by newlines. # use Switch 'Perl6'; # Import thedamian's sugar, it's better than C&H. use English '-no_match_vars'; # since we're using some Perl 6 syntax h +ere, may # as well get rid of $0 in the usage sta +tement my $file = shift or die "USAGE $PROGRAM_NAME filename\n"; open( FH, $file ) or die "Coudln't open $file: $!\n"; my $batchnum = 0; # global batch tracker while (<FH>) { chomp(); next if m/^$/; given ($_) { when /^FH$/ { print "File Header\n"; last; } when /^BH$/ { print "Batch Header\n"; $batchnum++; last; } when /^FT$/ { print "File Trailer\n"; last; } when /.*/ { handle_batch_content($_); } } } sub handle_batch_content { my $batch_content = shift; print "Got $batch_content in $batchnum\n"; # or whatever else you wa +nt to do }
Given the example file you provided, assuming that Example is newline delimited, this script produces:
File Header
Batch Header
Got 1234123 in 1
Got 1234123 in 1
Batch Header
Got 1234963 in 2
Got 1234963 in 2
Got 1234963 in 2
Batch Header
Got 1234999 in 3
Got 1234999 in 3
Got 1234999 in 3
Got 1234999 in 3
Got 1234999 in 3
File Trailer
HTH,
  dug

In reply to Re: Parse File With Sub While Loops by dug
in thread Parse File With Sub While Loops by Anonymous Monk

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.