Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Doubt in code - Beginner in perl

by Arik123 (Sexton)
on Dec 27, 2018 at 07:24 UTC ( #1227731=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1227731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2020-11-29 11:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?