Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何将perl中的字符串与变量而不是正则表达式匹配_Arrays_Regex_String_Perl_String Matching - Fatal编程技术网

Arrays 如何将perl中的字符串与变量而不是正则表达式匹配

Arrays 如何将perl中的字符串与变量而不是正则表达式匹配,arrays,regex,string,perl,string-matching,Arrays,Regex,String,Perl,String Matching,我有一个要使用perl检测的文件名数组 我还有很多,但这个例子只是为了说明: @toremove = (100, 102, 104, 131, 156); 现在我想打开目录并浏览文件,如果它们碰巧有上面数组中的任何文件名,我想采取一些措施 opendir (DIR, "D:/MYPATH/"); while (my $file = readdir(DIR)) { if($file =~ <how do i reference an array item here?>.txt

我有一个要使用perl检测的文件名数组

我还有很多,但这个例子只是为了说明:

@toremove = (100, 102, 104, 131, 156);
现在我想打开目录并浏览文件,如果它们碰巧有上面数组中的任何文件名,我想采取一些措施

opendir (DIR, "D:/MYPATH/");
while (my $file = readdir(DIR)) {
    if($file =~ <how do i reference an array item here?>.txt)
    { // do something }
opendir(DIR,“D:/MYPATH/”);
while(my$file=readdir(DIR)){
如果($file=~.txt)
{//做点什么}

有没有办法确定文件是否与数组中的任何项匹配?因此这更像是“变量字符串匹配”…

我要转换数组中的每个元素,然后使用智能匹配操作符:

#!/usr/bin/perl
use Modern::Perl;

my @toremove = (100, 102, 104, 131, 156);
my @txt = map{$_.'.txt'}@toremove;
while(<DATA>) {
    chomp;
    if ($_ ~~ @txt) {
        say "==> $_ : found";
    } else {
        say "$_ : NOT found";
    }
}

__DATA__
abc
def.txt
100
102.txt
131.abc
156.txt

我将变换数组的每个元素,然后使用智能匹配操作符:

#!/usr/bin/perl
use Modern::Perl;

my @toremove = (100, 102, 104, 131, 156);
my @txt = map{$_.'.txt'}@toremove;
while(<DATA>) {
    chomp;
    if ($_ ~~ @txt) {
        say "==> $_ : found";
    } else {
        say "$_ : NOT found";
    }
}

__DATA__
abc
def.txt
100
102.txt
131.abc
156.txt

我首先将数组转换为散列,然后简单地测试散列中是否存在密钥

my @toremove = (100, 102, 104, 131, 156);
my %lookup_hash;
$lookup_hash{"$_.txt"} = 1 for @toremove;

opendir (my $DIR, "D:/MYPATH/");
while (my $file = readdir($DIR)) {
    if( $lookup_hash{$file} )
    { // do something }

我首先将数组转换为散列,然后简单地测试散列中是否存在密钥

my @toremove = (100, 102, 104, 131, 156);
my %lookup_hash;
$lookup_hash{"$_.txt"} = 1 for @toremove;

opendir (my $DIR, "D:/MYPATH/");
while (my $file = readdir($DIR)) {
    if( $lookup_hash{$file} )
    { // do something }

每当你对自己说我怎样才能在列表中快速找到某样东西时,你应该对自己说:散列

让我们将该
@toremove
列表转换为散列。我们可以使用该命令执行此操作。该命令一开始有点吓人,我不相信手册页能做到这一点。它其实没有那么复杂:

 @some_hash_or_array = map { .... } @original_array;
获取
@original_array
并循环该数组的每个条目。
{…}
是一个小程序,您希望在每个条目上运行

你可以这样想:

my @original_array = qw( ... );
my @some_hash_or_array;
for my $entry ( @original_array ) {
    push @some_hash_or_array, { ... };
}
("100.txt" => 1, "102.txt" => 1, "104.txt" => 1, "131.txt" => 1, "156.txt" => 1)
有用的是,数组的每个条目都设置为
$\u
,因此操纵
$\u
可以变换数组

我正在做的是:

my %files = map { $_.".txt" => 1 } @to_remove;
当此命令处理
@以删除
时,它会创建另一个如下所示的数组:

my @original_array = qw( ... );
my @some_hash_or_array;
for my $entry ( @original_array ) {
    push @some_hash_or_array, { ... };
}
("100.txt" => 1, "102.txt" => 1, "104.txt" => 1, "131.txt" => 1, "156.txt" => 1)
我使用此数组初始化我的
%文件
哈希。奇数项是哈希的键,键后的偶数项是数据

#! /usr/bin/env perl
#
use strict;             # Lets you know when you misspell variable names
use warnings;           # Warns of issues (using undefined variables)
use feature qw(say);

my @to_remove = qw(100 102 104 131 156);

my %files = map { $_.".txt" => 1 } @to_remove;
while ( my $file = <DATA> ) {
    chomp $file;
    if ( exists $files{$file} ) {
        say qq(File "$file" is in the list);
    }
    else {
        say qq(File "$file" isn't in the list);
    }
}

__DATA__
100.txt
203.txt
130.txt
104.txt
150.txt
156.txt
160.txt
您需要执行以下操作:

#! /usr/bin/env perl
#
use strict;             # Lets you know when you misspell variable names
use warnings;           # Warns of issues (using undefined variables
use autodie;            # Automatically kills the program if opens fail
use feature qw(say);

my @to_remove = qw(100 102 104 131 156);
opendir my $dir_fh, "dir_name";

my %files = map { $_.".txt" => 1 } @to_remove;
while ( my $file = < $dir_fh > ) {
    if ( exists $files{$file} ) {
        say qq(File "$file" is in the list);
    }
    else {
        say qq(File "$file" isn't in the list);
    }
}
!/usr/bin/env perl
#
使用strict;#可以在拼写错误变量名时让您知道
使用警告;#警告问题(使用未定义的变量
使用autodie;#在打开失败时自动终止程序
使用特征qw(例如);
my@to_remove=qw(100 102 104 131 156);
opendir my$dir\u fh,“dir\u name”;
我的%files=将{$\.'.txt“=>1}@映射到\u移除;
而(我的$file=<$dir\u fh>){
if(存在$files{$file}){
说qq(文件“$File”在列表中);
}
否则{
说qq(文件“$File”不在列表中);
}
}

每当你对自己说“我怎样才能在列表中快速找到某个内容”,你都应该对自己说“散列!”

让我们将该
@toremove
列表转换为散列。我们可以使用该命令执行此操作。该命令一开始有点吓人,我不相信手册页能做到这一点。它其实没有那么复杂:

 @some_hash_or_array = map { .... } @original_array;
获取
@original_array
并循环该数组的每个条目。
{…}
是一个小程序,您希望在每个条目上运行

你可以这样想:

my @original_array = qw( ... );
my @some_hash_or_array;
for my $entry ( @original_array ) {
    push @some_hash_or_array, { ... };
}
("100.txt" => 1, "102.txt" => 1, "104.txt" => 1, "131.txt" => 1, "156.txt" => 1)
有用的是,数组的每个条目都设置为
$\u
,因此操纵
$\u
可以变换数组

我正在做的是:

my %files = map { $_.".txt" => 1 } @to_remove;
当此命令处理
@以删除
时,它会创建另一个如下所示的数组:

my @original_array = qw( ... );
my @some_hash_or_array;
for my $entry ( @original_array ) {
    push @some_hash_or_array, { ... };
}
("100.txt" => 1, "102.txt" => 1, "104.txt" => 1, "131.txt" => 1, "156.txt" => 1)
我使用此数组初始化我的
%文件
哈希。奇数项是哈希的键,键后的偶数项是数据

#! /usr/bin/env perl
#
use strict;             # Lets you know when you misspell variable names
use warnings;           # Warns of issues (using undefined variables)
use feature qw(say);

my @to_remove = qw(100 102 104 131 156);

my %files = map { $_.".txt" => 1 } @to_remove;
while ( my $file = <DATA> ) {
    chomp $file;
    if ( exists $files{$file} ) {
        say qq(File "$file" is in the list);
    }
    else {
        say qq(File "$file" isn't in the list);
    }
}

__DATA__
100.txt
203.txt
130.txt
104.txt
150.txt
156.txt
160.txt
您需要执行以下操作:

#! /usr/bin/env perl
#
use strict;             # Lets you know when you misspell variable names
use warnings;           # Warns of issues (using undefined variables
use autodie;            # Automatically kills the program if opens fail
use feature qw(say);

my @to_remove = qw(100 102 104 131 156);
opendir my $dir_fh, "dir_name";

my %files = map { $_.".txt" => 1 } @to_remove;
while ( my $file = < $dir_fh > ) {
    if ( exists $files{$file} ) {
        say qq(File "$file" is in the list);
    }
    else {
        say qq(File "$file" isn't in the list);
    }
}
!/usr/bin/env perl
#
使用strict;#可以在拼写错误变量名时让您知道
使用警告;#警告问题(使用未定义的变量
使用autodie;#在打开失败时自动终止程序
使用特征qw(例如);
my@to_remove=qw(100 102 104 131 156);
opendir my$dir\u fh,“dir\u name”;
我的%files=将{$\.'.txt“=>1}@映射到\u移除;
而(我的$file=<$dir\u fh>){
if(存在$files{$file}){
说qq(文件“$File”在列表中);
}
否则{
说qq(文件“$File”不在列表中);
}
}

这里有两个备选方案:

##  Shorthand hash definition
@toremove{100, 102, 104, 131, 156}= (1) x 5;

##  Using a glob instead of opendir
for (<D:/MYPATH/*>)
{
    ##  Use a regex for flexibility
    if (m:.*/(.+)$: and $toremove{$1})
    {
        print "Doing something to $_\n";
    }
}
##速记哈希定义
@toremove{100102104131156}=(1)x5;
##使用glob而不是opendir
对于()
{
##使用正则表达式实现灵活性
if(m:.*/(.+)$:和$toremove{$1})
{
打印“正在对$\n执行某些操作”;
}
}

这里有两个备选方案:

##  Shorthand hash definition
@toremove{100, 102, 104, 131, 156}= (1) x 5;

##  Using a glob instead of opendir
for (<D:/MYPATH/*>)
{
    ##  Use a regex for flexibility
    if (m:.*/(.+)$: and $toremove{$1})
    {
        print "Doing something to $_\n";
    }
}
##速记哈希定义
@toremove{100102104131156}=(1)x5;
##使用glob而不是opendir
对于()
{
##使用正则表达式实现灵活性
if(m:.*/(.+)$:和$toremove{$1})
{
打印“正在对$\n执行某些操作”;
}
}

smartmatch操作符已标记为。它们在5.20中再次更改,我强烈建议不要使用它,因为它在不断演变。smartmatch操作符已标记为。它们在5.20中再次更改,我强烈建议不要使用它,因为它在不断演变。错误的
@toremove{100、102、104、131、156}=(1)x5
谢谢,布拉德。我不知道最后四个元素会被初始化为空。错误您想要
@toremove{100102104131156}=(1)x5
谢谢,布拉德。我不知道最后四个元素会被初始化为空。