Why don't you check your match for success? If if fails, you get the
$1 from the previous iteration. That's probably not what you intended. Also, I'd use
\d+ instead of
\d* since we're not interested in matching
tim without any following digits. It probably should be anchored to the front of the string too, by the OP's spec.
my @wanted;
opendir my $dh, $dir or die "Failed opening $dir: $!";
foreach (readdir $dh) {
next unless /^tim(\d+)/;
my $num = $1;
push @wanted, $_ if $num > 14 and $num < 51;
}
closedir $dh;
Personally, I'd wrap the whole thing in a
do block and use
grep, so that I don't need to track my handles explicitly:
my @tim_file = do {
opendir my $dh, $dir or die "Failed opening $dir: $!";
grep {
my ($num) = /^tim(\d+)/;
defined $num and $num > 14 and $num < 51;
} readdir $dh;
};
Note that when you assign a match's captures to a list, failed expressions will return undef, so here I can implicitly test for success by testing the definedness of
$num.
Makeshifts last the longest.
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.