http://qs1969.pair.com?node_id=1227731


in reply to Doubt in code - Beginner in perl

I couldn't reproduce your error. I've run your code, and the output is exactly what you expected.

Are you sure you posted the code that generated the error?

The error seems to be that the newline is chomp'ed only from the first element of @words (which is "Perl\n"). Note that your output contains the string "Perl" twice - once with a newline (which appears two time in the input list) and once without a newline (which appears once. That is, the newline was chomp'ed only once). All the rest of the elements of @words appear in the output with a newline.

However, your code chomp(@words=<STDIN>) really does the work. It really chomp's the newline from all the elements. At least for me...

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

    Thank you for the reply , You are getting the correct output because you are probably providing the input to the program in different way then I am , your observation is correct , actually what's happening is that I am using the values in the array @words as keys in hash %count , now keys in hash are unique , but as you mentioned correctly that there are 2 "Perl" values , one with a newline and one without a newline , so it is treating them as separate keys , seems like chomp is not working on all the values , it's only removing newline from 1st input value. I have got the reason why my output is not correct because every line contains CRLF at the end and chomp(in my case) is removing only LF and keeping CR as correctly mentioned by @haukex