in reply to splitting the scalar variables in an array
split expects its second argument to be a string (i.e., a scalar), so using the array @TotalHosts here puts the array into scalar context, and split gets the number of elements in the array. You need to change this line:
my @dd = split "\n", @TotalHosts;
to something like this:
push @dd, split "\n", $_ for @TotalHosts;
which calls split on each element of the array in turn. This works provided that each of the array elements is a string.
Hope that helps,