This snippet makes a few assumptions and glosses over a few implementation details:
- Assume the input is well formed; "yes" or "no" always comes at the end of a complete module and ID definition sequence.
- Ignoring details about the file's "doc" encoding (which I assume you have already figured out).
- Ignoring details about your $Sheet object (again assuming you know what to do once data has been properly extracted).
Given those assumptions the following snippet will parse the file and place each "module/id" pair into an anonymous array, with all of them pushed onto an array poorly named @results.
use strict;
use warnings;
use autodie;
my $filename = 'something.doc';
my @results;
open my $in_fh, '<', $filename;
my( $module, $id );
while( my $line = <$in_fh> ) {
chomp $line;
if( $line =~ /^Module:\s+(\d+)/ ) {
$module = $1;
next;
}
if( $line =~ /^ID:(\d+)/ ) {
$id = $1;
next;
}
if( $line =~ /yes/ ) {
push @results, [ $id, $module ];
}
}
It may feel unclean allowing $module and $id to retain values from one iteration to the next, even after a 'no' in the 'Customer:' field. But as long as the input is well formed, $module and $id will always contain valid information at the moment a 'yes' is encountered.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.