I don't understand your "list of excluded characters". In the bash version you are using tr which only works on characters but in the Perl version your list uses two character strings instead of single characters. Also, Perl has a tr/// operator which operates pretty much the same as the command line tool (on single characters).

Your "list of excluded words" would probably be better as a hash.

Reading a file a line at a time shouldn't be a problem as Perl IO is buffered, however, you can change how much data is read in by changing the Input Record Separator ($/) to read in a whole file.

The comment in your program says "# remove non-letter characters", however you are only removing certain punctuation characters.

Perhaps you want something like this:

#!/usr/bin/perl # word counting program use strict; use warnings; use autodie; # list of excluded words my %excluded = map { $_ => 1 } qw( a about although also an and anothe +r are as at be been before between but by can do during for from has how however in in +to is it many may more most etc ); ## list of excluded characters #my @excluded_chars = ( "\\'", "\\:", "\\@", "\\-", "\\~", "\\,", "\\. +", "\\(", # "\\)", "\\?", "\\*", "\\%", "\\/", "\\[", "\\]", "\\=", '"' # ); # Read in whole file (uncomment next line) # local $/; my %count; # this will contain many words while (<>) { # remove punctuation tr{':@\-~,.()*%/[]="}{}d; foreach ( split ) { # remove excluded words next if exists $excluded{ lc() }; ++$count{ lc() }; # count each separate word } } foreach my $word (sort { $count{ $a } <=> $count{ $b } || $a cmp $b } +keys %count) { print "$count{$word} $word\n"; }

In reply to Re: Counting and Filtering Words From File by jwkrahn
in thread Counting and Filtering Words From File by maxamillionk

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.