The uninitialized value in hash element(!) would be $database. In such cases it is a good debugging technique to add statements like the following to your code to observe what happens:
use Data::Dumper; #at the start of the script
...
print Dumper(\$database,\$nextfield,\@fields);
$result{$database}{next}= $nextfield; $result{$database}{fields}= \@fi
+elds; #<--- the problematic line
A simple print "\$database is $database\n" would do as well, but Data::Dumper is to print for debugging like a lawnmower is to a scissor for cutting gras ;-).
Look at the output and compare it to your expectations. Probably you will see an undef value printed for $database. Now you can retrace the steps, look how $database could get that value, insert "print Dumper..." statements where you think it could go wrong. Maybe it is a regex that didn't match anything or maybe you put a value in an array into $a[1] but not in $a[0], which would leave the first array value undefined...