sub ReadSource{ my $DataSource = shift; my $HashRef = shift; foreach my $ItemID (@Data){ #Note @Data is not globally fed into the sub I have skipped the Data code to focus on the problem. if (exists($$HashRef{$ItemID})){ push @{$$HashRef{$ItemID}}, $DataSource; }else{ $$HashRef{$ItemID} = [$DataSource]; } } }