Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Chomp doesn't work

by M15U (Acolyte)
on Mar 25, 2013 at 08:48 UTC ( #1025253=perlquestion: print w/replies, xml ) Need Help??

M15U has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks

I have this rather silly problem with a piece of code :

I have a ".txt" file with the following lines :



Parameter 1

Parameter 2

There is no 'new line' between each line of the file.

And this code :

#!/usr/bin/perl -w use strict; use locale; use warnings; #use diagnostics; use utf8; binmode(STDIN, "encoding(utf8)"); binmode(STDOUT, "encoding(utf8)"); binmode(STDERR, "encoding(utf8)"); my $usr_input = "test/test.txt"; open (USRIN, '<:utf8', $usr_input) || die "Couldn't open $usr_input : +$!\n"; my @data_usr = <USRIN>; my $start_input = $data_usr[0]; my $end_input = $data_usr[1]; my $param1 = $data_usr[2]; my $param2 = $data_usr[3]; print "$start_input"; print "$end_input"; print "$param1"; print "$param2"; close (USRIN);

Everything works, but I always seem to get a new line in my variables. I tried everything : chomp (@data_usr), or chomping first URSIN in a while loop, or chomping each new variable. It seems that each time chomp removes all the characters not only the "\n" one.

What am I doing wrong ? I know it's a really newbie question.

Thank you

Replies are listed 'Best First'.
Re: Chomp doesn't work
by davido (Cardinal) on Mar 25, 2013 at 09:04 UTC

    chomp strips from its parameter list the contents of $/, which is typically "\n". However, there are at least five Unicode code points that produce a newline (see perluniprops) Also, "\n" means different things on different machines; a file saved on a Windows system will have a different version of "\n" than a file saved on a Unix/Linux system. This could be getting in your way too.


      I got it. It's a little embarasing : each end of line has"\r\n" at the end. So by using this replace statement it works :

      $start_input =~ s/\r\n//d;

      So, if you ever get something similar, think about the "\r" before the "\n".

        Just use \R instead!
        $start_input =~ s/\r\n//d;

        /d is not a valid option for s///.    Perhaps you meant tr/\r\n//d?

Re: Chomp doesn't work
by daxim (Curate) on Mar 25, 2013 at 08:59 UTC
    chomp @data_usr; works for me. Look at your data with a hex viewer or uniquote.
    $ hex test/test.txt
    0000  31 39 39 38 2e 30 33 2e  32 35 2e 30 31 0a 31 39  1998.03.25.01␊19
    0010  39 39 2e 31 30 2e 30 31  2e 30 31 0a 50 61 72 61␊Para
    0020  6d 65 74 65 72 20 31 0a  50 61 72 61 6d 65 74 65  meter 1␊Paramete
    0030  72 20 32 0a                                       r 2␊
Re: Chomp doesn't work
by demerphq (Chancellor) on Mar 25, 2013 at 14:26 UTC

    Off topic, but I strongly recommend you NOT use locale;


      Why is it recommend not "use locale"? I was searching for an explanation but I didn't found anything about debating on using or not using it.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1025253]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-05-20 17:16 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (75 votes). Check out past polls.