Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

"chomp" not working

by cuautemoc (Initiate)
on May 02, 2012 at 23:17 UTC ( #968582=perlquestion: print w/replies, xml ) Need Help??
cuautemoc has asked for the wisdom of the Perl Monks concerning the following question:

So, the chomp function isn't working as I expect. I'm reading a string into an array and using the split function to split the input on the \n, which is working fine. However, once I have the array, I want to get rid of the \n. My line chomp(@array) isn't chomping the \n.

Here are the lines from my code. The arrays are exactly the same when printed out. What am I missing?

my @arr1 = split/\n/,$var; print "@arr1\n"; chomp(@arr1); print "@arr1\n";

Replies are listed 'Best First'.
Re: "chomp" not working
by tinita (Parson) on May 03, 2012 at 08:29 UTC
    as always in such a case, I'd recommend to look what you really have in your variable. with a normal print you only see printable characters.
    use Data::Dumper; local $Data::Dumper::Useqq = 1; # will also print \r, \n etc. print Dumprt $var;
    from HTML forms you typically get a carriage return and a newline.
Re: "chomp" not working
by JavaFan (Canon) on May 02, 2012 at 23:20 UTC
    What is in $var, what are the results of the print, and what are you expecting?

    I expect both prints to print out the same. After all, the chomp has nothing to do, as there will be no newlines left after the split.

      $var is taking input from an HTML text area. It goes something like this:

      a,b,c,d e,f,g,h i,j,k,l

      There is a carriage return at the end of each line. @arr1 before the chomp should like like above when printed out. However, I would expect that after the chomp that @arr1 would print like this:

      a,b,c,d e,f,g,h i,j,k,l

      But it doesn't, it prints like the first example.

        There is a carriage return at the end of each line. @arr1 before the chomp should like like above when printed out.
        You are splitting on newlines. Why do you expect them to still be there?

        @arr1 before the chomp should like like above when printed out.

        Why would you expect that?

        >perl -wMstrict -le "my $s = qq{a,b,c,d\ne,f,g,h\ni,j,k,l}; printf qq{$s}; printf qq{\n}; ;; my @ra = split /\n/, $s; printf qq{@ra}; printf qq{\n}; ;; chomp @ra; printf qq{@ra}; printf qq{\n}; " a,b,c,d e,f,g,h i,j,k,l a,b,c,d e,f,g,h i,j,k,l a,b,c,d e,f,g,h i,j,k,l
Re: "chomp" not working
by bobf (Monsignor) on May 03, 2012 at 02:05 UTC
Re: "chomp" not working
by Marshall (Abbot) on May 03, 2012 at 05:45 UTC
    There may be an issue with "\n" in the regex sense. Perl is actually pretty "smart" about various line endings when using the file I/O layer.

    I/O input is very tolerant about read (accepts any of the flavors of line ending and chomp operates fine). Perl I/O uses the context appropriate value for write. For example on Unix, printing to a file gives just <LF>, on Windows printing to a file give <CR><LF> and printing to a socket (any OS) gives <CR><LF> - the network standard text line ending.

    The "\n" here may be just matching <LF>(Line Feed) instead of <CR><LF> (Carriage Return, Line Feed)? I can't think of any other way for the reported symptoms to happen.

    I suppose a different regex could be used or another possibility is to open the $var for read and use the IO Layer to deal with different line endings. Of course there is a performance penalty for using this extra layer, but that may not matter.

    The below code will read any type of line ending (even if they are mixed).

    #!/usr/bin/perl -w use strict; my $input=<<END; a,b,c,d e,f,g,h i,j,k,l END # a ref to a Perl variable can be opened for reading! # open IN, '<', \$input or die "cannot open var for reading"; while (<IN>) { chomp; print; } #a,b,c,de,f,g,hi,j,k,l __END__ To convert files from Windows that I transfer to Unix: This seemingly nonsensical program converts the line endings... when reading a Windows file on Unix... while(<>) { chomp; # gets rid of <CR><LF> or just<LF> or just <CR> print "$_\n"; # new line ending is just <LF> }
    Anyway the above "nonsensical program" is how I convert a Perl script from a Windows machine for printing on a Unix machine. Perl itself does not care if the Perl source code contains mixed <LF> and <CR><LF> endings, but lpr on Unix does! So this is a simple filter that I use to get rid of any <CR> values before printing some script that might have been edited on my Windows machine and transferred to Unix.
Re: "chomp" not working
by ikegami (Pope) on May 04, 2012 at 20:58 UTC

    chomp doesn't change the array because there is no newline in the array. split returns what's separated by the separator.

    +------------------> $arr1[0] = "aaa"; | +-------------> $arr1[1] = "bbb"; | | +--------> $arr1[2] = "ccc"; | | | +----> empty field dropped | | | | --- --- --- - aaa\nbbb\nccc\n

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://968582]
Approved by sauoq
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-04-23 20:02 GMT
Find Nodes?
    Voting Booth?