Greetings,
I came across some odd behavior when using the print function and I was curious to see if someone could explain to me why it happened. In my script, I read in a file that contained lines with two strings connected with an equal sign (e.g. string1=string2).
As I read in each line, I chomped off the newline character at the end, split on the equal sign, and then created a hash where the key was the second string and the value was the first.
I would then loop through the hash like this:
foreach my $item (sort keys %hash){
print "The $item is $hash{$item}\n";
}
I was expecting it to print:
The key is value
But what I got was:
is value
After pulling my hair out, I finally realized that the file I was reading in was created in Windows so each line ended with a carriage return. The chomp only removed the \n and left the \r. So each key had a \r at the end of it.
Once I removed the \r character it printed out just fine.
My question is why did the print function not print anything before the \r character?
Thanks!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.