File::Find losing one file?

S

seven.reeds

Hi,

I have a directory hierarcy that is holding some "news" articles [a
local term for some loosely related files]. The hierarchy is based on
the date of creation: ./YYYY/MM/DD/<article_number>.news

I am using File::Find to crawl through the dirs to find the ".news"
files. As it finds a news file it appends the full pathname to an
array. I know that there are X news files in the tree but (X-1) are
ever returned from File::Find.

Ideas are welcome, please

I am doing:

use File::Find;
my @articles = ();
find({wanted => \&findArticle, no_chdir => 1}, ".");
warn "#### $#articles\n";
....

sub findArticle()
{
if (-f && /\d+.news/)
{
push(@articles, $_);
}
}
 
B

Brian Helterline

seven.reeds said:
Hi,

I have a directory hierarcy that is holding some "news" articles [a
local term for some loosely related files]. The hierarchy is based on
the date of creation: ./YYYY/MM/DD/<article_number>.news

I am using File::Find to crawl through the dirs to find the ".news"
files. As it finds a news file it appends the full pathname to an
array. I know that there are X news files in the tree but (X-1) are
ever returned from File::Find.

Ideas are welcome, please

I am doing:

use File::Find;
my @articles = ();
find({wanted => \&findArticle, no_chdir => 1}, ".");
warn "#### $#articles\n";

$#articles returns the last index of @articles which is X-1 elements
in the array since arrays start with index 0.

If you want the number of elements in @articles, evaluate the array in
scalar context;

my $nelements = @articles;
or
my $nelemenets = $#articles + 1;
 
M

Marc Espie

sub findArticle()
{
if (-f && /\d+.news/)
{
push(@articles, $_);
}
}

You know that your regexp is wrong, right ? It will catch up more
stuff than you want because:
- . stands for `any character', not just .
- you don't anchor it at all.

This expr will also catch stuff like
this42anewsletter
which might or might not be what you want...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top