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

Hello Monks, this is driving me crazy.. I have a line looks like this :
one % : thrre blblblb % 545334
How can I eleminate the \n from the end of the line? Is it the spaces is causing this ?

Replies are listed 'Best First'.
Re: chomp not working
by ikegami (Patriarch) on Feb 15, 2006 at 22:12 UTC

    What do you mean by "chomp [is] not working"? chomp removes \n (or whatever $/ holds) form the end of the string, if present.

    I'd say the likelyhood of chomp not working is nil. The following questions should help you find the real problem:

    • Does $/ contain \n (and just that)?
      Test: die("Bad \$/\n") if $/ ne "\n";
    • Does the string end with \n?
      Test: die("String doesn't end with \\n\n") if $line !~ /\n\z/;
    • Is there only one \n at the end of the string?
      Test: die("String has multiple \\n\n") if $line =~ /\n.*\n/s;
    • Are you sure there isn't any other special characters (such as \r) at the end of the string or before the \n?
      Test: die("String has \\r\n") if $line =~ /\r/;

    Update: Added test snippets.

      I don't if the string end with line , I am giving the output as it is and trying to print the line as it is then next to the line I need to write valid or not valid . how ever if I try to type valid next to the line it will go to the next line no matter what I do :
      print "$line valid\n";
      will look somthing like this one % ...... valid
        Run
        $line = "one % : thrre blblblb % 545334\n"; chomp($line); print "$line valid\n";

        If you get

        one % : thrre blblblb % 545334 valid

        then someting is really wrong with your perl.

        If you get,

        one % : thrre blblblb % 545334 valid

        then chomp is working and my previous post stands. Answer the questions in my previous post if you want to find the problem. I've just added code snippets in that post to help you answer the questions.

Re: chomp not working
by japhy (Canon) on Feb 15, 2006 at 22:13 UTC
    There are probably characters there you don't expect. Try using something like 'od' or 'cat -vet' to see the "invisible" characters in your file.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Could this be a Windows to UNIX text file transfer problem? Hidden characters used in Windows text files can do strange things in UNIX. There is an EMBOSS program that will strip these for you.
Re: chomp not working
by GrandFather (Saint) on Feb 15, 2006 at 22:32 UTC

    How about posting a small code snippet that shows the problem. I suggest you use the following template as a starting point:

    use strict; use warnings; while (<DATA>) { chomp; print ">$_<\n"; } __DATA__ test data that chomps fine. Your manky data that fails for a chomp A nicely chomped line

    Prints:

    >test data that chomps fine.< >Your manky data that fails for a chomp< >A nicely chomped line<

    DWIM is Perl's answer to Gödel
Re: chomp not working
by Fletch (Bishop) on Feb 15, 2006 at 22:12 UTC
    $ perl -e '$_=qq{one % : thrre blblblb % 545334\n};chomp;print "!!$_!! +\n"; print "Erm...\n"' !!one % : thrre blblblb % 545334!! Erm . . .