in reply to matching characters in filename

Your question is a bit unclear to me - what do you mean by "match"? Do you have a directory full of files, and you are trying to locate files whose name match a certain pattern? Here's a quick example of one way to do it (there are plenty other ways, but this works fine if everything is in one directory), this uses glob to list and preselect files and then a regular expression to filter out only those that exactly match the format you specified:

for my $file (glob '1M01_F*.npt.gro') { next unless $file=~/^1M01_F\d{5}\.npt\.gro$/; print "$file\n"; } __END__ 1M01_F00121.npt.gro 1M01_F00130.npt.gro 1M01_F00140.npt.gro 1M01_F00150.npt.gro

... or, are you just trying to generate filenames? Like:

for my $n (121,130,140,150) { my $file = sprintf '1M01_F%05d.npt.gro', $n; print "$file\n"; } __END__ 1M01_F00121.npt.gro 1M01_F00130.npt.gro 1M01_F00140.npt.gro 1M01_F00150.npt.gro

There are a few other issues with your script, such as that the first line should start with #!/ with no space at the beginning and no slash at the end, you should be using warnings, and if you're not interested in capturing the output of the grompp command, you should use system instead of backticks (``). Since your regular expressions also aren't going to work, I suggest you have a look at perlintro and perlretut, and the other tutorials linked from there.

Replies are listed 'Best First'.
Re^2: matching characters in filename
by Anonymous Monk on Aug 11, 2015 at 13:34 UTC

    ... and in the first example, if you want to limit the files by the number that appears in the filename, one way to do that is:

    for my $file (glob '1M01_F*.npt.gro') { next unless $file=~/^1M01_F(\d{5})\.npt\.gro$/ && $1>120 && $1<=150; print "$file\n"; } __END__ 1M01_F00121.npt.gro 1M01_F00130.npt.gro 1M01_F00140.npt.gro 1M01_F00150.npt.gro

    The doubling of the filename pattern may not be particularly elegant though. Other ways to list files in a directory include readdir, File::Find (which goes into subdirectories too), Path::Class, or Path::Tiny.