English 中文(简体)
如何使用 Per 寻找特定档案,使其名称的格局相匹配
原标题:How to use Perl s grep to find a particular file by matching a pattern in its name

我储存在Perl变量中,应与在名录中储存的档案名称的开头部分相匹配。

我利用这一变量找到从目录中匹配这一模式的文档,使用Perl的颗粒。 我在此做的是:

    opendir (DIR, "data/testroot/") or die "$!";
    @file1 = <$f1/*.hdf>
    foreach(@file1){
       $patt = substr(basename($_),0,$ind);
       $file2 = grep {/${patt}*.hdf/} readdir(DIR);
       #other code follows.......
    }
    closedir(DIR);

首先,我收到文件夹f1的所有档案清单,并在文件夹@file上储存。 然后在@file1上填写。 我摘录了头几种特性,将其储存在$patt上,然后试图从另一个文件夹上收集类似的文档,这些文件有 patt所储存的对应起始模式。

The grep $file2 = grep{/${-{patt}*.hdf/} Readdir (DIR); is not work.

问题回答

我认为,你想在名录A中找到所有<代码>*.hdf的档案,其档案名称与第一个<代码>_$ind相符。

您应同时使用<代码>glob或readdir,但不得同时使用。 在这种情况下,glob 看来是最佳星号,因为它允许你选择所有<代码>*.hdf。 A号案的档案无需与监管机构核对。

The program below seems to do what you need. I have substituted sample values for $f1 and $ind.

use strict;
use warnings;

use File::Basename;

my $f1 =  data ;
my $f1 =  data/testroot ;
my $ind = 6;

foreach (glob "$f1/*.hdf") {
  my $patt = substr(basename($_), 0, $ind);
  my @match = glob "$f2/$patt*.hdf";

  #other code follows.......
}

${patt}*.hdf means "0 or more occurrences of $patt, followed by .hdf". Are you sure you don t mean "$patt, followed by arbitrary text, followed by .hdf"?

这将是<代码>/${patt}.*.hdf/。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签