Reputation: 2951
I want to pass a hash and a variable to a subroutine:
%HoA = {'1'=>'2'};
my $group_size = 10;
&delete_unwanted(\%HoA,$group_size);
sub delete_unwanted {
my (%HoA,$group_size) = @_;
print "'$group_size'\n"
}
But, this prints nothing.
Upvotes: 0
Views: 62
Reputation: 35218
You're passing a hash reference (as you should), so therefore assign it to a scalar in your parameter catching:
sub delete_unwanted {
my ($hashref, $group_size) = @_;
print "'$group_size'\n"
}
If you later want to dereference it, you can my %newHoA = %$hashref;
, but that will be a copy of the original hash. To access the original structure, just use the reference: print $hashref->{a_key};
.
Upvotes: 5
Reputation: 300
Your problem is in:
my (%HoA,$group_size) = @_;
You can solve it by saying, for example:
sub delete_unwanted {
my $hashPointer = shift;
my $group_size = shift
Note that you can retrieve the original hash inside the subroutine by either: de-referencing the hashPointer (my %HoA = %$hashPointer), or you can access the hash contents directly using the pointer directly (eg, $hashPointer->{'key'})
Upvotes: 1