I agree that you need a flag to determine whether or not you are in a block, but beyond that, you need a datatype to hold all your data. I think you want an array of blocks that contains an array of lines that consists of an array of elements. This ought to do the trick:
use Data::Dumper;
sub readFile{
open my $fh, "resource/datafile.txt" || die "Cannot open: $!";
@lines = <$fh>;
$inblock = 0;
my @block = [];shift @block;
while($#lines >= 0){
my $single = shift @lines;
print $single;
if($single =~ /END DATA/){
$inblock = 0;
push @allblocks,[@block];
@block = [];shift @block;
}
push @block,[split ' ',$single] if $inblock;
$inblock = 1 if($single =~ /START DATA/);
}
}
readFile;
print Dumper(\@allblocks);
When a block starts the flag begins, and when a block ends the block is added to
@allblocks. I don't consider myself a monk yet, but I tried the code and it works.
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.