访问传递的哈希值的数组元素

发布于 2024-12-11 15:23:43 字数 1815 浏览 0 评论 0原文

我有两个哈希值,其中文件夹名称作为键,其各自的文件作为数组。但我无法访问 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 技术交流群。

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

发布评论

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

评论(2

南…巷孤猫 2024-12-18 15:23:43

在 getMissingFiles() 中,正如您取消引用 $hash1$hash2 来获取键一样,您也需要取消引用它们来获取值:

@folder_files = @{ $hash1->{$folder1} }; 

或者,

@folder_files = @{ $hash1{$folder} };

您可以这样做可以获取单个文件:

$file = $hash1->{$folder}[$i];

In getMissingFiles(), just as you dereference $hash1 and $hash2 to get the keys, you also need to dereference them to get the values:

@folder_files = @{ $hash1->{$folder1} }; 

or alternatively,

@folder_files = @{ $hash1{$folder} };

And you can do this to get individual files:

$file = $hash1->{$folder}[$i];
彡翼 2024-12-18 15:23:43

该调用语法不太正确 - 您想要

my ($hash1, $hash2) = @_;

或者也许

my $hash1 = shift;
my $hash2 = shift;

shift 函数只会给您第一个值,因此您需要按照您的建议调用两次,或者访问参数列表 @_ 如果您想要一次性采摘到比价值更多的东西。

That call syntax isn't quite right - you want

my ($hash1, $hash2) = @_;

or perhaps

my $hash1 = shift;
my $hash2 = shift;

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.

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