in reply to create array of empty files and then match filenames
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:
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
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: create array of empty files and then match filenames
by angela2 (Sexton) on Jan 07, 2016 at 17:51 UTC | |
by kcott (Archbishop) on Jan 07, 2016 at 19:01 UTC | |
by jnbek (Scribe) on Jan 08, 2016 at 16:19 UTC |