为什么我从 readdir 中得到的是数字而不是文件名列表?
我有一段 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
perldoc grep 说:
这正是你正在做的事情。因此,您找到了 1 个文件,并将该结果分配给
$dirs_found
变量。perldoc grep says:
And that's exactly what you are doing. So you found 1 file and that result is assigned to
$dirs_found
variable.问题是,您正在将
grep
评估为标量上下文,将其更改为列表上下文将为您提供所需的结果。在标量上下文中,
grep
返回表达式为真的次数。在列表上下文中,它返回表达式为真的元素。
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.
为什么要打开目录并查找特定的文件名?如果您想查看该文件是否存在,只需直接测试它即可:
但是,当您遇到此类问题时,请检查每个步骤的结果以检查您得到的结果。您的第一步将分解问题陈述:
一旦您这样做,您就会发现问题是
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:
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:
Once you do that you see that the problem is
grep
. Once you know that the problem isgrep
, 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. :)你应该说
@dirs_found
,而不是$dirs_found
you should say
@dirs_found
, not$dirs_found