foreach my $e (@{$getToysResults->{Toys}{Toy}}) { my $location_ref = $e->{ToyLocations}{ToyLocation}; my $ref_type = ref $location_ref; my $location_array_ref = $ref_type eq 'ARRAY' ? $location_ref : $ref_type eq 'HASH' ? [ $location_ref ] : $ref_type eq 'SCALAR' ? [ { locationName => $$location_ref } ] : $ref_type eq '' ? [ { locationName => $location_ref } ] : undef; die 'Data corrupt' unless defined $location_array_ref; for my $location_array_hash_ref (@$location_array_ref) { print $location_array_hash_ref->{locationName}, "\n"; } }