while(<$fh>) {
This reads the file in line-by-line. The current line of the file is stored in $_
. It's basically the same as while($_ = <$fh>) {
. Technically it expands to while(defined($_ = <$fh>)) {
, but they're very close to the same thing (and either way, it's automatic, so you don't need to worry about it).
@tmp = split;
"split
" with no arguments is (mostly) equivalent to "split /\s+/, $_
". It splits the current line into a list of items between whitespace. So it splits the current line into a list of words (more or less) and stores this list in an array. However, this line is bad. @tmp
should be qualified with my
. Perl would catch this if you have use strict;
and use warnings;
at the top.
push @AoA, [@tmp];
}
This pushes a reference to an anonymous array containing the elements that were in @tmp
into @AoA
, which is an array of arrays (as you probably already knew).
So in the end, you have a list @AoA
where each element in the list corresponds to a line of the file, and each element of the list is another list of the words on that line.
In short, @tmp
should really be declared using my
, and you should use strict;
and use warnings;
. In fact, as has been said, you could do away with @tmp
altogether:
while(<$fh>) { push @AoA, [split] }
But using a temporary array may be nicer on anyone who has to add to this code later.
EDIT: I missed the regex you wanted to add:
while(<$fh>) {
last unless /^[\d\s]*$/;
push @AoA, [split];
}
However, /^[\d\s]*$/
won't catch all integers - specifically, it won't match -1
. If you want it to match negative numbers, use /^[\d\s-]*$/
. Also, if you want to match non-integers (floating-point numbers), you could use /^[\d\s\.-]*$/
, but I don't know if you want to match those. However, these regexes will match invalid entries like 1-3
and 5.5.5
, which are NOT integers or numbers. If you want to be more strict about that, try this:
LOOP: while(<$fh>) {
my @tmp = split;
for(@tmp) {
# this line for floating points:
last LOOP unless /^-?\d+(?:\.\d+|)$/;
# this line for just integers:
last LOOP unless /^-?\d+$/;
}
push @AoA, [@tmp];
}