in reply to Doubt in code - Beginner in perl

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.

Replies are listed 'Best First'.
Re^2: Doubt in code - Beginner in perl
by Perl_Programmer1992 (Sexton) on Dec 27, 2018 at 09:47 UTC

    Thank you so much ! , you are spot on , the theory you mentioned in the first part of your reply is exactly what's happening with my code , I am actually using an online perl compiler and feeding the input from that(not from any file) , and as you correctly mentioned that every line contains CRLF at the end and chomp is only taking care of LF , and that's why the hash %count is treating the 2 "Perl" input in array as separate instead of treating them as unique , you provided so many valuable things in your reply and it will really help me on my journey to become a good perl programmer , once again thank you :-)