Dear Monks,
I have searched high and low and cannot find the
answer to this problem I am having.
To demonstrate here's my code:
#!/usr/bin/perl -w
use strict;
my @names;
my @items;
sub processRec {
my $fh = shift; #ref to filehandle
while(<$fh>) {
chomp;
s/\cM//;
last if /^$/;
last if /^\d-RECORD$/;
push ( @names, /ITEMNAME-(.*)/);
push ( @items, /ITEM \d-(.*)/);
}
}
open( FH, "<data");
while(<FH>) {
if ( /\d-RECORD/ ) { processRec(*FH); }
}
close(FH);
for my $name ( @names ) { print "name = [$name]\n"; }
for my $item ( @items ) { print "item = [$item]\n"; }
This works fine most of the time.
But here is a case where it doesn't work ...
$ cat data
1-RECORD
ITEMNAME-bob
ITEM 1-socks
ITEM 2-shoes
2-RECORD
ITEMNAME-bill
ITEM 1-hat
ITEM 2-hair piece
3-RECORD
ITEMNAME-mary
ITEM 1-roach clip
4-RECORD
ITEMNAME-jimmy
ITEM 1-peanuts
ITEM 2-tooth pick
Here's the output from that case
name = bob
name = bill
name = jimmy
item = socks
item = shoes
item = hat
item = hair piece
item = peanuts
item = tooth pick
What happened to mary?
If there was a new line after the line ...
ITEM 2-hair piece
... I wouldn't have a problem.
I suspect that I need to do something like this ...
sub processRec {
...
if (/^\d-RECORD$/) {
put_line_back ($_);
last;
};
...
}
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.