G'day angela2,
Welcome to the Monastery.
"Can anybody tell me what I'm doing wrong???"
There's many issues with the code you've posted:
-
For a start, you don't get any filenames to process.
How does your script know which directory to look in?
Where does it read the filenames (i.e. get a directory listing)?
How does it differentiate between filenames that are plain files, those that are directories, and those that are something else (e.g. symbolic links)?
-
Your regex, /(\d+)/, is matching strings containing one or more digits (and capturing the digits into $1 which you don't use); you want strings containing only digits (i.e. /^\d+$/).
Furthermore, that regex by itself is effectively a no-op: something like 'next unless /^\d+$/' would perhaps be more meaningful.
-
You declare both $digit and $emptyname but don't initialise them.
That's absolutely fine: they both start as undef.
You never change $digit: it remains undef throughout.
You set $emptyname to $digit on every iteration: so it too remains undef throughout.
-
You never populate @emptyfiles: it has zero elements.
Consequently, your foreach loop has zero iterations, i.e. that loop is never entered.
-
You also have a problem with what I assume was intended to be a shebang line, i.e. #/bin/perl/.
That's just a comment!
Shebang lines start with #!.
Also, /bin/perl/ looks like a directory and is probably wrong.
To be honest, and I don't mean this in any sort of nasty way, I rather think you just threw code at the problem and hoped it would work instead of having any real idea of what was going on.
Accordingly, I think you'd be well served by reading "perlintro -- a brief introduction and overview of Perl": it's not particularly long and should really help you to understand the code you're writing.
The next step is how to resolve these issues.
Take a look at the readdir function to "get a directory listing".
You'll see in the first example it uses the -f file test (to check for plain files); like the -z you used in your code (to check for files of zero size).
[For reference, here's all the unary file test operators.]
That example also uses a regex: I addressed your regex above (i.e. /^\d+$/).
Making a small modification to that example, and putting it in a script:
#!/usr/bin/perl -l
use strict;
use warnings;
use autodie;
my $dir = '.';
opendir(my $dh, $dir);
my @emptyfiles = grep { -f && /^\d+$/ && -z } readdir $dh;
closedir $dh;
print for @emptyfiles;
With these files available (and 12 being the only one with any content):
$ ls -l 1 12 123
-rw-r--r-- 1 ken staff 0 8 Jan 03:00 1
-rw-r--r-- 1 ken staff 7 8 Jan 03:00 12
-rw-r--r-- 1 ken staff 0 8 Jan 03:00 123
Running that script, gives this output:
1
123
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.