Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Print Behavior with Carriage Return

by johngg (Canon)
on Jan 05, 2020 at 12:33 UTC ( #11110973=note: print w/replies, xml ) Need Help??


in reply to Print Behavior with Carriage Return

Note that you can open a file with CRLF line endings for reading with the :crlf IO layer. chomp will then remove both line ending characters.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MData::Dumper -E + ' my $fileStr = qq{val1=key1\r\nval2=key2\r\nval3=key3\r\n}; open my $inFH, q{<:crlf}, \ $fileStr or die $!; my %hash; while ( <$inFH> ) { chomp; my( $v, $k ) = split m{=}; $hash{ $k } = $v; } close $inFH or die $!; print Data::Dumper ->new( [ \ %hash ], [ qw{ *hash } ] ) ->Sortkeys( 1 ) ->Dumpxs();' %hash = ( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3' );

I hope this is helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Print Behavior with Carriage Return
by haukex (Chancellor) on Jan 05, 2020 at 18:33 UTC
    chomp will then remove both line ending characters.

    Nitpick: I/O layers don't change the behavior of chomp, it will continue to remove $/, which is typically one character, "\n", even on Windows. The I/O layer converts \r\n to \n when reading, so the strings end in just "\n" before they are even touched by chomp.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2020-06-07 03:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (42 votes). Check out past polls.

    Notices?