Welcome to Perl and the Monastery, Perl_Programmer1992!

The way you've shown it here, some of the output is split onto two lines, and because of that I would guess that some of your strings have extra newlines at the end. You've used chomp, which is good, but it's possible that, for example, your input has CRLF line endings, and chomp is only removing the LF, not the CR. To check this, it's usually best to use a module like Data::Dumper, which normally comes installed with Perl. At the top of your program, add the lines use Data::Dumper; and then $Data::Dumper::Useqq=1; (the latter to make the output more helpful for this case), and then at the end of the program add print Dumper(\%count);. Then, if you see output where the strings look like, for example, "Perl\r", that means my theory was correct, as the \r represents the CR (\n is LF). If it's something else and you're not sure, feel free to post the output here, inside <code> tags. Another way to verify this, if you're on a *NIX system, is to use a program like hexdump or od to show the files, e.g. hexdump -C input.txt or od -tx1c input.txt - if you see 0d 0a, that's a CRLF and again, if you have doubts, show the output here, inside <code> tags.

Now, as for how to fix it, I'd suggest maybe converting the input to use LF line endings. I doubt that your terminal will use CRLF when you type the input into it directly*, so I suspect that probably you have an input file that you are piping to Perl (such as cat input.txt | perl script.pl or perl script.pl <input.txt, so it's best to edit the file. One way to do this is to open the file in a text editor that supports different line endings, often they will have an option when saving the file to change them. You'd have to tell us which editor you're using for more help on that. Another option is to use a program like Tofrodos, which often comes installed on *NIX systems or can be installed (for example on Debian or Ubuntu, sudo apt-get install tofrodos), then you can use the command fromdos input.txt to change CRLF line endings to LF.

One other option is to change the program itself, although I would really only recommend this if you know you'll be dealing with mixed line endings. You can remove extra whitespace from the end of the strings in @words by saying s/\s+\z// for @words; just after the chomp - this will loop over the strings, and the regular expression will remove any whitespace at the end of the string (for details on this, see perlsyn and perlretut). There is another alternative involving the special variable $/, but since that only applies when your know for sure that your input will always have CRLF line endings, we don't need to get into that here.

Here are some more tips:

* Update: To be more specific, on Windows, Perl will normally translate CRLF to LF on its own, so that your Perl strings should still only contain LF line endings, which is why I've been guessing that your input is coming from a file (on *NIX), but please correct me if I'm wrong.


In reply to Re: Doubt in code - Beginner in perl by haukex
in thread Doubt in code - Beginner in perl by Perl_Programmer1992

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.