use strict; use warnings; my @list =qw (1 2 3 4 5); # define a hash to take back the values my %returned_values; # get the array references my ($keys, $values) = apackage::ahash(@list); # Put them in a hash @returned_values{@{$keys}} = @{$values}; # print out for my $key (@{$keys}){ print "$key maps to $returned_values{$key} \n"; } package apackage; sub ahash{ my %hash; # You are calling the subroutine with an uneven number of elements, # You should check for this and correct before assigning to a hash if ((@_ % 2) != 0){ push (@_,"null"); } %hash = @_ ; # Your foreach loop made no sense here my @ke = keys %hash; my @val = values %hash; # return references to your two arrays return (\@ke, \@val); # in this context returning a hash makes more sense }