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


in reply to Need help with a simple perl script

Like others, I cannot reproduce your error with the code you've posted. Using Perl 5.14.2 and Mac OS X 10.7.4, I get:

BLA|001036|S|3228|10|1|2|3|001036|W035|S| |4 CHL|123777|S|3327|3|0|0|0|001036|W035|S| |2 BLT|600123|S|3437|0|20|0|0|001036|W035|S| |0 BRO|900177|S|3531|-1|0|0|0|001036|W035|S| |0

You asked "How do I get the count value to not print to a new line?". However, that's not really what's happening. Taking the first line of what you're getting, this actually looks like:

BLA|001036|S|3228|10|1|2|3|001036|W035|S|<space><some-return-char>|4<n +ewline>

The last pipe character comes from $"="|";; the 4 is your count value; the terminal <newline> is the \n from the print statement. The bogus <some-return-char> comes from the while loop's $_ value. I can reproduce your output by adding my own bogus return character:

... while( <DATA> ){ chomp; $_ .= qq{\n}; ...

which now outputs:

BLA|001036|S|3228|10|1|2|3|001036|W035|S| |4 CHL|123777|S|3327|3|0|0|0|001036|W035|S| |2 BLT|600123|S|3437|0|20|0|0|001036|W035|S| |0 BRO|900177|S|3531|-1|0|0|0|001036|W035|S| |0

You'll need to determine where the bogus return is added: it could be when the file is initially populated or perhaps due to some subsequent processing it undergoes before you read it. I've encountered this problem in the past when data is copied and pasted from an email, when data is transferred via FTP in binary mode and similar scenarios involving systems using different line endings.

I would suggest the following as your options:

  1. Fix the problem with the source data and leave your code as it is.
  2. Run the source data through some cleaning filter before you read it and leave your code as it is.
  3. Edit your code to remove any bogus returns immediately after the chomp.

-- Ken