When you compare both the methods, in the second method i didn't hard code the column headings directly in my script. But the html output which i am getting is not like as method1. Could you please any one suggest me a solution in which it should be generic not like method1.
Please suggest me without using any modules like Text::CSV. I need the solution with out using modules.

NOR do I see any ready explanation of how your code(1) could have produced this output. Did you cut and paste accurately? And what, in your view, causes the second to fail as it does? That may (update: already answered by jethro) help you ascertain your problem without further coaching.

Finally, perhaps you can explain what you mean by "...it should be generic not like method1."