Last string appears at beginning of line in formatted output

Viewed 43

Has anyone any idea why the following would format itself in a weird way? In several years I've had no problem with creating simple text output but this problem has me baffled.

I'm using the line

print "$BC,$Ttl,$FN,$SN,$Finalage,$OurLoc,$OurDT,$FinalPC\n";

Every value is a simple text string on which I've run "chomp" to remove return characters.

I would expect the output to look like the following:

*DD10099999,,Information Services,Guest Ticket 2,41,C G,03/11/2020,NE8 9BB*

$BC is the first item and $FinalPC is the postcode at the end.

Instead I get:

*,NE8 9BB99, ,Information Services,Guest Ticket 2,41,C G,03/11/2020*

The final item has somehow moved to the beginning of the line and overwritten the first item. This is happening consistently on every line of my screen and text file output and I'm completely stumped as to why. The data is read from a text file and compared with database output which is also simple text. There are no occurrences of \b anywhere in my code. Why would a backspace character get into it?

1 Answers

The string in $OurDT ends with a carriage return, which causes your terminal to home the cursor. Presumably, the value of $OurDT came from a Windows file read on a unixy machine.

One option is to fix the file (e.g. by using the dos2unix utility).

Another is to accept both CRLF and LF as line endings (e.g. by using s/\s+\z// instead of chomp).

Related