Replaced $_ with explicit variable name $datum

$_ is a global variable so using it twice in nested loops can cause problems. When I tried to run the code with the nested $_ it did not work as intended.