in reply to problem in printing the result in the out put file

Maybe your input file has unexpected line endings, like \r. Try to get rid of them with:
foreach my $ip (@ip) { $ip =~ s/\s+//;

Tip #3 from the Basic debugging checklist