in reply to script optimization
You may want to read How do I post a question effectively?, some sample data would be nice. See also Debugging and Optimization and Devel::NYTProf regarding profiling and benchmarking.