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
    I feel a bit bad about myself now lol! Anyway, I hope I'll learn. At some point. In all honesty yes I did hope it would work, but I din't just throw it in, I spent quite a lot of time on this rubbish code I wrote. I have completed a Perl tutorial and I have read perlintro, so obviously I'm just not very talented in coding if I manage to make one million mistakes in 10 lines. I'll try to study all your suggestions now, thank you very much Ken.

      Don't feel bad about yourself or think you're no good at coding. None of us were born knowing Perl; you've just started learning whereas I've been coding in Perl for over two decades: it's not unreasonable to expect that I might be a bit better at it than you. :-)

      — Ken

      Yes, as Ken posted, we all started out as n00bs. Never let that concern you, the goal is learning and one never really stops learning coding, unless they give up on coding outright. Just keep at it and you'll have many a 'lightbulb' moment where what you thought was a huge hurdle suddenly makes perfect sense and you've conquered it. Apply that to everything you write from then on out and keep searching for those lightbulb moments.

      There's a saying floating around the internet somewhere, I can't remember where I first saw it, but, regardless of the language, it's gospel truth:
      If you can't look at code you wrote 6 months ago and be completely terrified at what you wrote, you did not learn enough.

      That's the beauty and the curse of programming, there's always something new to learn and conquer

      --- Where Earth and Spirit Unite.