foreach my $name(keys %($xml_hash->{reaction}} ){ next unless name =~ m@^m:@; my $substrate_id = $xml_hash->{reaction}->{$name}->{substrate}->{id}; my $product_id => $xml_hash->{reaction}->{$name}->{product}->{id}; # do stuff here... }