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

Your input data probably contains an extra \r that is not removed by chomp. You can see that with Data::Dump:

use Data::Dump qw( pp ); # some code here print pp \@ip; chomp(@ip); print pp \@ip;
One of the ways this can happen is when you try to read a file created on Windows with perl running on Linux, or a Cygwin perl.

You can correct that by explicitly setting the input record separator to the CRLF format:

my @ip; { local $/ = "\r\n"; # because this is local, the normal behaviour is +restored after this block open my $dat, "<", $in_file or die "Can't open file $in_file: $!"; @ip = <$dat>; chomp(@ip); close $dat; }

Replies are listed 'Best First'.
Re^2: problem in printing the result in the out put file
by johngg (Canon) on Feb 28, 2017 at 19:12 UTC

    Another way to correct it is to use open with the :crlf IO layer (if that's the correct term):

    johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my $file = qq{Line 1\r\nLine 2\r\nLine 3\r\n}; open my $inFH, q{<:crlf}, \ $file or die $!; while ( <$inFH> ) { chomp; say qq{>$_<}; } close $inFH or die $!;' >Line 1< >Line 2< >Line 3<

    I hope this is of interest.

    Cheers,

    JohnGG

      I hope this is of interest

      It sure is! I actually find this method better than modifying $/, it is easier to understand and you don't have to worry about contaminating the way other handles are read. I did read about it a while ago, but I had completely forgotten this is something you can do, thanks for the reminder.