I'm stuck on a problem. I'm trying to make references for 2 hashes, then compare them in a subroutine. However, there is an error:
Can't use an undefined value as a HASH reference at compareHashes.pl line 10.
My code is:
use strict;
use warnings;
use feature qw(say);
my $hash_1; my $hash_2;
compareHashes($hash_1, $hash_2);
sub compareHashes{
say "the first hash:", "
", %$hash_1;
say "the second hash:", "
", %$hash_2;
if ((keys(%$hash_1) eq keys(%$hash_2)) and (values(%$hash_1) eq values(%$hash_2))){
say "Two above hashes are equal";}
else {
say "Two above hashes are not equal";
}
};
my %hash1 = (ITALY => "ROME",
FRANCE => "PARIS"
);
my %hash2 = ( ITALY => "MILAN",
FRANCE => "PARIS"
);
my %hash3 = (ITALY => "ROME" );
my %hash4 = (SPAIN => "ROME",
FRANCE => "PARIS"
);
my $hash1_r = \%hash1;
my $hash2_r = \%hash2;
my $hash3_r = \%hash3;
my $hash4_r = \%hash4;
compareHashes ($hash1_r, $hash1_r);
compareHashes ($hash1_r, $hash2_r);
compareHashes ($hash1_r, $hash3_r);
compareHashes ($hash1_r, $hash4_r);
Please tell me what is wrong. I appreciate your help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…