Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: problem in printing the result in the out put file

by Eily (Monsignor)
on Feb 28, 2017 at 16:22 UTC ( [id://1183160]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1183160]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 18:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found