in reply to Perl script to delete files using regex

If, as it appears, you are trying to find a file with a fixed name rather than a group of files with a common part to their name, no regexp is required. Just perform a string comparison on the filename.

 if ($file eq 'carlos.txt')

This script will list everything in the directory where the script is located and point out the file named 'carlos.txt'

use strict; my $file; # Read the current directory into an array opendir(my $dir, '.'); my @files = readdir($dir); closedir ($dir); # Iterate over array to file file foreach $file(@files) { print "$file"; if ($file eq 'carlos.txt') { print " <------------- Found File"; } print "\n"; }

Replies are listed 'Best First'.
Re^2: Perl script to delete files using regex
by GrandFather (Saint) on Nov 24, 2020 at 00:52 UTC

    For filtering lists grep is often used:

    use warnings; use strict; opendir(my $dir, '.'); my ($file) = grep {$_ eq 'carlos.txt'} readdir($dir); closedir ($dir); print "$file <------------- Found File\n" if $file;
    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Yes indeed - much nicer and much more Perl-ish!

      I deliberately didn't write it that way though so that CarlosN could (hopefully) understand what is going on.

        Clarity is rather in the eye of the beholder. But as a general thing smaller code (as long as it's not obfuscated) is easier to unpack. In this case there are two Perlish things that are likely to trip up a novice. The first is grep which is easy to find documentation for. The second is the list assignment which is kinda hard to formulate a good search for!

        The saving grace is for novices who don't know what they don't know the assignment looks kinda innocuous and it's likely their eyes just glaze over for a moment as they recognize it as a wobbly assignment and move on. For this example that's pretty much OK because the real lesson is filtering a list. That said, YMMV!

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond