For a start

while (@thisfile) {

doesn't do what you think. In particular, it is not

for (@thisfile) {

Next:

$seen{b} <=> $seen{a}

compares the values for keys 'a' and 'b', not for the two sort variables' ($a and $b) contents as you are hoping. Cleaning those problems up, fixing a few other style issues and providing some sample data gives:

use strict; use warnings; my $fileData = <<DATA; Greetings, I've been trying to put a word frequency counter together but I keep g +etting an unitialised value in pattern match. I'd be grateful if somebody could +let me know what I'm dong wrong. I added a line to get some repeated words. DATA my %seen; open my $inFile, '<', \$fileData; for (grep {chomp; length} <$inFile>) { $seen{lc $1}++ while /(\w['\w-]*)/g; } close ($inFile); printf "%5d %s\n", $seen{$_}, $_ for sort { $seen{$b} <=> $seen{$a} } +keys %seen;

Prints:

2 a 2 to 2 i 1 i've 1 know 1 put 1 if 1 unitialised 1 greetings 1 i'd 1 frequency 1 wrong 1 let 1 could 1 in 1 keep 1 line 1 repeated 1 trying 1 what 1 value 1 me 1 match 1 grateful 1 i'm 1 word 1 be 1 some 1 somebody 1 but 1 added 1 words 1 dong 1 been 1 get 1 together 1 getting 1 pattern 1 counter 1 an

Perl reduces RSI - it saves typing

In reply to Re: Word Frequency counter by GrandFather
in thread Word Frequency counter by Anonymous Monk

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.