以下结构不起作用是否有任何理由?文件列表包含文件名。名称列表包含一个名称列表,当它们作为文件名的子字符串匹配时,会导致循环将文件移动到名为$ name的目录。它似乎应该工作,但它不是移动文件。有什么更好的方法来构建它?
FILE: for my $file (@file_list) {
for my $name (@name_list) {
if ($file =~ /^\Q$name\E/) {
rename "/Users/path/to/file/I/need/to/move/$file", "/Users/path/to/directory/i/need/to/move/file/to/$name/$file" or die "rename failed because: $!\n";
next FILE;
}
}
print "no match for $file\n";
}
答案 0 :(得分:0)
正确的代码:
for my $file (@file_list) {
my $found = 0;
for my $name (@name_list) {
if ($file =~ /^\Q$name\E/) {
print "failed to rename $file\n" unless rename "/Users/path/to/file/I/need/to/move/$file", "/Users/path/to/directory/i/need/to/move/file/to/$name/$file";
$found = 1;
last;
}
}
print "no match for $file\n" unless $found;
}