in reply to different length of a line from linux and windows textfile?

Most probably a problem with trailing newlines.

Try to chomp your input $line.

Cheers Rolf

( addicted to the Perl Programming Language)

  • Comment on Re: different length of a line from linux and windows textfile?

Replies are listed 'Best First'.
Re^2: different length of a line from linux and windows textfile? ("\r")
by tye (Sage) on Mar 17, 2014 at 15:03 UTC

    A complaint about different line length from Windows files is likely due to "\r", which chomp will do nothing to.

    s/\s+$//

    Will trim all trailing whitespace, which is what I recommend over chomp. It catches "\n" and "\r" as well as spaces and tabs (which should never be allowed to have significance at the end of a line where they are extra invisible).

    - tye        

      You are right that chomp wont pick up \r on its own, however it absolutely will pick up \r\n and correctly remove both characters, so the only time this would be an issue is if your file is corrupted, or specifically crafted to utilise \r in some way.

        chomp removes \r\n only on MSWin. On Linux, it only removes \n:
        ~$ perl -E '$x = "|\r\n";print $x;chomp $x; print $x' | xxd 0000000: 7c0d 0a7c 0d |..|.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ