Problems and suggestions:
- Simplify your grep by using the /i flag to make it case-insensitive.
- Since the foreach loop puts each filename in $_, $ucount is completely superfluous and likely to confuse things.
- You put a matched filename in $frameu and then....do nothing with it.
- You print the filenames that don't match.
- You increment the counter, but can you be certain the counter will always correspond to the number in the filename?
- I think what you want is to find the largest-numbered file in the directory, and then print the next-largest number.
So, rewriting with those things in mind:
#!/usr/bin/perl
use strict;
use warnings;
my $topdir = 'D:/PGN/ELSEVIER/FNS/1(1)/press';
opendir HU, $topdir; # don't quote a single scalar
my @files = grep /\.txt$/i, readdir HU; # will also match .TXT
closedir HU;
my $biggest = 0; #keep track of largest number found
for my $frame (@files){
if($frame =~ m|^partial_pressps_report(\d+)|){
my $n = $1; # capture number from filename
if( $n > $biggest ){ # is it bigger than $biggest?
$biggest = $n; # then save it
}
}
}
$biggest++; # got the largest one, now increment it
print $biggest; # and print it
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.