in reply to Re: Re: Re: Reading from databse and writing to file.
in thread Reading from databse and writing to file.

I'm guessing that line 51 is this one:
If $ncount gt 0;
First, "if" should not be capitalized. Next, parens will help. Thirdly, use "==" ">" rather than "gt" for numeric comparison. (I think only the first point is mandatory; Perl is probably trying to interpret "If" as some sort of function call.)

Apart from that, try formatting for legibility -- sensible and regular indents, breaking up long lines, and so on. It really cut down on the amount of time spent fixing things.