in reply to Problem in Commenting lines in perl

Your inner loop:
foreach $ip(@ip) { print $out "#" if(/\b$ip\b/i && !/^#/); #only one # at front of lin +e print $out $_; }
Calls "print $out" once for each item in @ip, causing double lines. The simplest fix (untested) is early exit:
foreach $ip(@ip) { last if /^#/; # ALready has a hash . Note - LAST exists this loop next unless /\b$ip\b/i; # No match - so try the next item print $out "#"; last; # Remember to exit loop after one successful match } print $_; # Print onloy once.
When you are ready, take the next step and learn regex alternation, for a more efficient solution.

     Syntactic sugar causes cancer of the semicolon.        --Alan Perlis