G'day Carol,

Welcome to the Monastery.

What you're describing is typically caused by embedded characters. A carriage-return ("\r") is the most usual culprit:

$ perl -e 'my $x = "abc\r"; print "|$x|"' |abc

Without the carriage-return ("\r") the problem of losing the final character ("|") disappears:

$ perl -e 'my $x = "abc"; print "|$x|"' |abc|

The problem usually arises when processing data from one OS (operating system) on a different OS. They have different line-endings: "\n" (Unix-style systems, including Linux & Mac OS X); "\r" (Mac versions earlier than Mac OS X); "\r\n" (MSWin).

The problem may not be a carriage-return but some other control character; for instance, something like an embedded backspace could potentially cause similar problems. Use ord to identify which, if any, embedded characters may exist:

$ perl -E 'my @x = ("A", "B\n", "C\r", "D\r\n"); for (@x) { say ord fo +r split //; }' 65 66 10 67 13 68 13 10

Say, on a Unix-like system, you have an entire, unconverted record from an MSWin file that looks like: "abc\r\n". Using chomp (which, on a Unix-like system, will consider a newline to be the line-ending character) will only remove the terminal "\n" resulting in "abc\r" (which I used in the original example).

$ perl -E 'my @x = ("A", "B\n", "C\r", "D\r\n"); for (@x) { chomp; say + ord for split //; }' 65 66 67 13 68 13

In these cases, I often find it useful to remove the generic line-ending (\R) — see perlrebackslash: \R for more information.

$ perl -E 'my @x = ("A", "B\n", "C\r", "D\r\n"); for (@x) { s/\R$//; s +ay ord for split //; }' 65 66 67 68

Do note that you'll need Perl 5.10 to use \R. If you have an older version of Perl, you can do this:

$ perl -E 'my @x = ("A", "B\n", "C\r", "D\r\n"); for (@x) { s/[\r\n]*$ +//; say ord for split //; }' 65 66 67 68

Finally, as this is your first post, I won't harp on it too much but, if you provide more information, you'll generally get a better answer that doesn't involve a lot of guesswork. See "How do I post a question effectively?" and "SSCCE".

— Ken


In reply to Re: Perl appears to be dropping last character of line by kcott
in thread Perl appears to be dropping last character of line by cmarra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.