in reply to How do I re-read a line?
The idea is to store the names and the items into the hash, and when a new record is read, simply store the hash. The problem is that you have (well, i have to - this is an invitation for a more elegant solution should someone wish to show me a new trick ;)) discard the first line and store the last record outside of the while loop:
If someone knows of a more elegant way, please share. I tried using File::ReadBackwards, but then extra work is needed to reverse the items ...use strict; use Data::Dumper; my (@record, $discard, %temp); open(FH, 'data') or die 'file not found'; # discard the first line $discard = <FH>; while(<FH>) { chomp; s/\cM//; if (/^\d-RECORD$/) { push @record, {%temp}; undef %temp; } elsif (/ITEMNAME-(.*)/) { $temp{name} = $1; } elsif (/ITEM \d-(.*)/) { push @{$temp{item}}, $1; } } # the last record needs to be added outside while loop push @record, {%temp}; print Dumper \@record; # print items of last record print join(', ', @{$record[-1]{item}}), "\n";
UPDATE:
Regarding your regexes - you might want to consider
changing \d to \d+ so that you can catch numbers greater
than 9, and you probably should use \s or \s+
instead of a literal space.
UPDATE: UPDATE:
Thanks belg4mit!
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (jeffa) Re: How do I re-read a line?
by belg4mit (Prior) on May 01, 2002 at 22:42 UTC |