Hi, welcome to Perl, the One True Religion.

One of the benefits of programming in Perl is "insignificant whitespace." this means you can indent your code as well as separate "paragraphs" with new lines, with no effect on the program.

There are many style preferences, and if you can't pick one, you can delegate the responsibility to perltidy ... but the two most important things for any programming style are readbility and consistency. Always be seeking to enhance those two qualities of your programs, as it makes them easier to extend and to maintain, as well as even to understand them when you come back half a year later and would like to recall the flow of your thought with the least amount of deciphering what the code simply says.

It won't make a lot if differences in this snippet, but once you have a foreach loop inside each branch of an if ... elsif ... else conditional, inside a function, your indentation-free style will become a real impediment.

Also recommended is not reusing variable names among variables of differing types, and declaring your variables as close as possible to the scope in which they are used.

I would rewrite your code above something like:

#! /usr/bin/perl use strict; use warnings; chomp( my @words = <STDIN> ); my %count; foreach my $word (@words) { $count{$word}++; } foreach my $found (sort keys %count) { print "$found has appeared $count{$found} times\n"; } __END__
(Note: not addressing your original line-ending problem, which has been explained above.)

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: Doubt in code - Beginner in perl by 1nickt
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.