访问传递的哈希值的数组元素
我有两个哈希值,其中文件夹名称作为键,其各自的文件作为数组。但我无法访问 getMissingFiles 子中传递的哈希的数组元素(请参阅错误消息的注释)。
要比较的哈希值:
# contains all files
%folderWithFiles1 =
(
foldername1 => [ qw(a b c d e f g h i j k l m n o p) ],
foldername2 => [ qw(a b c d e f g h i j k l m n ) ],
)
%folderWithFiles2 =
(
foldername1 => [ qw(a b d e h i l m n p) ],
foldername2 => [ qw(a d f g h j m ) ],
)
比较子例程(从 hash2 中获取不在 hash1 中的缺失文件):
sub getMissingFiles()
{
my ($hash1, $hash2) = shift; # is it working?
#my $hash1 = shift; # or do you have to do it separately?
#my $hash2 = shift;
my $flag = 0;
my @missingFiles;
foreach my $folder (sort(keys %{$hash1}))# (sort(keys %hash1)) not possible?
{
for (my $i = 0; $i < @$hash1{$folder}; $i++)
{
foreach my $folder2 (sort(keys %{$hash2}))
{
foreach my $file2 (@$hash2{$folder2})
{
if ($hash1{$folder}[$i] == $file2) # Error: Global symbol "%hash1" requires explicit package name
{
$flag = 1;
last;
}
}
if (0 == $flag)
{
push(@missingFiles, $hash1{$folder}[$i]); # Error: Global symbol "%hash1" requires explicit package name
}
else
{
$flag = 0;
}
}
}
}
return @missingFiles;
}
调用函数:
@missingFiles = &getMissingFiles(\%hash1, \%hash2);
- Is:“my ($hash1, $hash2) = shift;”正确还是必须单独进行?
- 为什么“foreach my $folder (sort(keys %hash1))”不可能?
- 有没有比使用 4 个循环更有效的方法?
I have got two hashes with a foldername as key and its respective files as an array. But I cannot acces the array elements of the passed hash in the getMissingFiles sub (see comments for error message).
Hashes to be compared:
# contains all files
%folderWithFiles1 =
(
foldername1 => [ qw(a b c d e f g h i j k l m n o p) ],
foldername2 => [ qw(a b c d e f g h i j k l m n ) ],
)
%folderWithFiles2 =
(
foldername1 => [ qw(a b d e h i l m n p) ],
foldername2 => [ qw(a d f g h j m ) ],
)
Compare subroutine (get missing files from hash2 that are not in hash1):
sub getMissingFiles()
{
my ($hash1, $hash2) = shift; # is it working?
#my $hash1 = shift; # or do you have to do it separately?
#my $hash2 = shift;
my $flag = 0;
my @missingFiles;
foreach my $folder (sort(keys %{$hash1}))# (sort(keys %hash1)) not possible?
{
for (my $i = 0; $i < @$hash1{$folder}; $i++)
{
foreach my $folder2 (sort(keys %{$hash2}))
{
foreach my $file2 (@$hash2{$folder2})
{
if ($hash1{$folder}[$i] == $file2) # Error: Global symbol "%hash1" requires explicit package name
{
$flag = 1;
last;
}
}
if (0 == $flag)
{
push(@missingFiles, $hash1{$folder}[$i]); # Error: Global symbol "%hash1" requires explicit package name
}
else
{
$flag = 0;
}
}
}
}
return @missingFiles;
}
Calling function:
@missingFiles = &getMissingFiles(\%hash1, \%hash2);
- Is: "my ($hash1, $hash2) = shift;" correct or do you have to do it separately?
- Why is "foreach my $folder (sort(keys %hash1))" not possible?
- Is there a more efficient way than using 4 loops?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 getMissingFiles() 中,正如您取消引用
$hash1
和$hash2
来获取键一样,您也需要取消引用它们来获取值:或者,
您可以这样做可以获取单个文件:
In getMissingFiles(), just as you dereference
$hash1
and$hash2
to get the keys, you also need to dereference them to get the values:or alternatively,
And you can do this to get individual files:
该调用语法不太正确 - 您想要
或者也许
shift 函数只会给您第一个值,因此您需要按照您的建议调用两次,或者访问参数列表
@_
如果您想要一次性采摘到比价值更多的东西。That call syntax isn't quite right - you want
or perhaps
The shift function will only give you the first value, so you need to call twice as you suggest, or access the parameter list
@_
if you want to pluck more than value in one go.