for my $index2 (0..$#productarray) { $Prod{$reference}->{$productarray[$index2]}++; } Output: 'ref261' => { 'rope' => '4', 'suit' => '1', 'boats' => '3', 'Gloucestershire' => '1', }, 'ref523' => { 'pool' => '2', 'fish' => '1', 'polystyrene' => '1', },