use strict; my %hash1 = (1=>[5,4,3,2,1], 2=>[1,2,3], 3=>[8,9]); my %hash2 = (1=>[6,5,4,3,2], 3=>[4,5]); foreach my $key1 (keys %hash1) { if (exists $hash2{$key1}) { foreach my $ss (0..@{$hash1{$key1}}) { ## do subtraction here using $ss as subscripts: ## my $data1 = $hash1{$key1}[$ss] - $hash2{$key1}[$ss]; ## it's not clear from your code why you're subtracting ## keys from values. } } }