http://qs321.pair.com?node_id=1128976


in reply to Strings mangled on printing

Check to see if $rt has a carriage return character at the end of it. Perhaps add something like $rt =~ s/\r//g; to strip any out.

Replies are listed 'Best First'.
Re^2: Strings mangled on printing
by atcroft (Abbot) on Jun 03, 2015 at 18:12 UTC

    $rt = $inp; chomp $rt; # insert at line 23 $rt =~ s/Remote Temp://;

    chomp() would probably be more appropriate, since it removes (from the docs) "any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the 'English' module)", and $/ can be set to the appropriate value if there is a difference in line endings/separators (such as "\n" vs. "\r" vs "\r\n" vs. ...).

    Hope that helps.

      atcroft:

      Normally, I'd agree with you, but since the data is coming over a serial port, I wouldn't expect the line endings to be consistent with those used on whatever platform runs the code. So in this case, I'd prefer explicit removal of junk from the end.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Re^2: Strings mangled on printing
by gordu (Initiate) on Jun 03, 2015 at 17:51 UTC

    Arrgh!!!! So simple. I should have asked years ago. Yes that fixed it. Thank you!

      Also note that $inp =~ s/\s+$//; will remove all trailing white space, including newlines (\n) and returns (\r).

      Update: Correct typo (per reply).

        Actually, $inp =~ s/\s$//; will remove only a single whitespace character at the end of the line.

        Use $inp =~ s/\s+$//; to remove all of them.