UPDATE 2 - I have posted the final script (final as in it's what I just turned it in - bear in mind this is an introductory class :) at the bottom of this post. Thanks again!

UPDATE - Wow! I came home and found all this great help! I appreciate all of it and will try to absorb it all. It should be said that this is for an intro Perl class, so things like proper use of strict; and deciding what a "word" is are kinda like swimming laps, whereas I'm currently just trying to float and not drown! I will say that in posting here at Perlmonks I have learned more in a shorter time than I ever have in any class...many thanks...and now back to my script. I'll update here with what I end up with.

Also known as "CTP learns a new stupid Perl trick"

So as some of you may remember, I'm working on a concordance script, and almost had it licked, until the end where I need to print out the top ten occuring words. I figured I should start a new thread since I've come to a whole new problem. Here's the script so far:

#! usr/bin/perl use warnings; open (TEXTFILE, 'text.txt') or die ("Cannot open file : $!"); $big_string = <TEXTFILE>; $big_string = lc ($big_string); $big_string =~ s/\(|\)/ /g; @words = split (/[,.\s+]/, $big_string); foreach (@words){ push @gt_three_char_words, $_ if /[a-zA-Z]{4,}|[a-zA-Z]{3,}'/; } foreach (@gt_three_char_words) { $hash{$_}++; } foreach $key (sort {$hash{$b} <=> $hash{$a}} keys %hash) { for ($i=0; $i<=9; $i++) { print "$key\t\t= $hash{$key}\n"; }}
The thing that I'm stuck on is the printing the top ten hits. The way it's written right now I get ten of the top word, ten of the next, ten of the next, etc, all the way thru the list. If I move the for loop outside I get the whole list ten times over.

How the heck do I just print the first ten? Again - thanks in advance!

btw - yes, we have already covered that I should be using strict; and declaring my variables, that I should really be slurping my text file (although the script currently does what it should thru some kludgy perl magic ;), and a few other things I need to fix before I turn it in. :-)
#! usr/bin/perl #midterm part 1 use warnings; use strict; my @gt_three_char_words = (); my %hash = (); open (TEXTFILE, 'text.txt') or die ("Cannot open file : $!"); my $big_string = <TEXTFILE>; $big_string = lc ($big_string); $big_string =~ tr/()//d; my @words = split (/[,.:;!?\s]+/, $big_string); foreach (@words) { push @gt_three_char_words, $_ if /[a-zA-Z]{4,}|[a-zA-Z]{3,}'/; } foreach (@gt_three_char_words) { $hash{$_}++; } foreach my $key ( (sort {$hash{$b} <=> $hash{$a} } keys %hash ) [0..9] + ) { print "$key\t\t= $hash{$key}\n"; }

In reply to Tutelage, part two by ctp

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.