Reputation: 1123
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);
Upvotes: 0
Views: 161
Reputation: 78155
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.
Upvotes: 1
Reputation: 6798
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];
Upvotes: 1