Thank you very much psini and that was fast.
the only problem in the program was $i which was not localised.
i must change $i to my $i.
but i also observed that this was a lot slower than compared to a c++ program and took a lot of time.but anyway it worked perfectly.
thanks again