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
No, allow me to repeat: $i as global was the main problem in your program, it prevented the program to work.
But it is not the only problem: all the other things I mentioned are time bombs waiting for the first time you change something in the code to explode.
Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."