为什么我从 readdir 中得到的是数字而不是文件名列表?

发布于 2024-09-26 15:02:10 字数 896 浏览 0 评论 0原文

我有一段 Perl 代码,用于搜索目录并在找到匹配项时显示该目录的内容。代码如下:

$test_case_directory = "/home/sait11/Desktop/SaLT/Data_Base/Test_Case";
$xml_file_name = "sample.xml"

$file_search_return = file_search($xml_file_name);
print "file search return::$file_search_return\n";


sub file_search
{
    opendir (DIR, $test_case_directory) or die "\n\tFailed to open directory that contains the test case xml files\n\n";

    print "xml_file_name in sub routines:: $xml_file_name\n";

    $dirs_found = grep { /^$xml_file_name/i } readdir DIR;
    print "Files in the directory are dirs_found :: $dirs_found\n";
    closedir (DIR);
    return $dirs_found;
}

输出是,

xml_file_name in sub routines:: sample.xml
Files in the directory are dirs_found :: 1
file search return::1

它没有返回找到的文件名。相反,它始终返回数字 1。

我不知道为什么它不返回目录中存在的名为 sample.xml 的文件名。

I have a piece of Perl code for searchnig a directory and display the contents of that directory, if match is found. The code is given below:

$test_case_directory = "/home/sait11/Desktop/SaLT/Data_Base/Test_Case";
$xml_file_name = "sample.xml"

$file_search_return = file_search($xml_file_name);
print "file search return::$file_search_return\n";


sub file_search
{
    opendir (DIR, $test_case_directory) or die "\n\tFailed to open directory that contains the test case xml files\n\n";

    print "xml_file_name in sub routines:: $xml_file_name\n";

    $dirs_found = grep { /^$xml_file_name/i } readdir DIR;
    print "Files in the directory are dirs_found :: $dirs_found\n";
    closedir (DIR);
    return $dirs_found;
}

Output is,

xml_file_name in sub routines:: sample.xml
Files in the directory are dirs_found :: 1
file search return::1

It is not returning the file name found. Instead it returns the number 1 always.

I don't know why it is not returning the file name called sample.xml present in the directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断念 2024-10-03 15:02:10

perldoc grep 说:

在标量上下文中,返回表达式为 true 的次数。

这正是你正在做的事情。因此,您找到了 1 个文件,并将该结果分配给 $dirs_found 变量。

perldoc grep says:

In scalar context, returns the number of times the expression was true.

And that's exactly what you are doing. So you found 1 file and that result is assigned to $dirs_found variable.

可是我不能没有你 2024-10-03 15:02:10
($dirs_found) = grep { /^$xml_file_name/i } readdir DIR; #capture it

问题是,您正在将 grep 评估为标量上下文,将其更改为列表上下文将为您提供所需的结果。

标量上下文中,grep 返回表达式为真的次数。

列表上下文中,它返回表达式为真的元素。

($dirs_found) = grep { /^$xml_file_name/i } readdir DIR; #capture it

Problem was that, you were evaluating the grep as scalar context, change it to list context will give you the desired result.

In scalar context, grep returns the number of times the expression was true.

In list context, it returns the elements for which the expression was true.

倾听心声的旋律 2024-10-03 15:02:10

为什么要打开目录并查找特定的文件名?如果您想查看该文件是否存在,只需直接测试它即可:

 use File::Spec::Functions;
 my $file = catfile( $test_case_directory, $xml_file_name );
 if( -e $file ) { ... }

但是,当您遇到此类问题时,请检查每个步骤的结果以检查您得到的结果。您的第一步将分解问题陈述:

 my @files = readdir DIR;
 print "Files are [@files]\n";

 my $filtered = grep { ... } @files;
 print "Files are [$filtered]\n";

一旦您这样做,您就会发现问题是 grep。一旦您知道问题出在 grep 上,您就可以阅读它的文档,注意到您使用了错误的方法,这样您就可以比在 StackOverflow 上发布问题更快地完成任务。 :)

Why are you opening a directory and looking for a particular filename? If you want to see if the file is there, just test for it directly:

 use File::Spec::Functions;
 my $file = catfile( $test_case_directory, $xml_file_name );
 if( -e $file ) { ... }

When you run into these sorts of problems, though, check the result at each step to check what you are getting. Your first step would decompose the problem statement:

 my @files = readdir DIR;
 print "Files are [@files]\n";

 my $filtered = grep { ... } @files;
 print "Files are [$filtered]\n";

Once you do that you see that the problem is grep. Once you know that the problem is grep, you read its documentation, notice that you are using it wrong, and you're done sooner than it would take to post a question on StackOverflow. :)

悲歌长辞 2024-10-03 15:02:10

你应该说@dirs_found,而不是$dirs_found

you should say @dirs_found, not $dirs_found

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文