Hi,

The following code is hanging up the browser and I think it has something to do with the hash having keys array and values array. There is no error message, just the browser taking a really long time. Any help would be appreciated. Works fine with 3 line, 12 word 1 kb text file but not with 7 kb text file.

#SNIP_OF_CODE_WHERE_HANGING_UP_BROWSER
my %uniqueHash = ();
my @uniqueArray;
my @countsArray;
@[EMAIL PROTECTED] = @countsArray;


for my $word (sort sort_values keys %uniqueHash) { $percentage = $uniqueHash{$word}/($totalcount); printf "%-15s %5d %5.3f \%\n", "$word", $uniqueHash{$word}, $percentage*100; if ($uniqueHash{$word} >= $mostvalue) { $mostvalue = $uniqueHash{$word}; $mostArray[$count] = $word; $mostValues[$count] = $uniqueHash{$word}; $mostHash{$mostArray[$count]} = $mostValues[$count]; $count++; } }

sub sort_values { $uniqueHash{$b} <=> $uniqueHash{$a} }
#SNIP_END

#ALL_CODE
#!/usr/bin/perl -w
#file: wordcount2.cgi

use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';
use strict;

print "Content-type: text/html \n\n";

my $file = param('upload');
my %uniqueHash = ();
my @uniqueArray;
my @countsArray;
@[EMAIL PROTECTED] = @countsArray;

print_results() if param;

sub sort_values { $uniqueHash{$b} <=> $uniqueHash{$a} }

sub print_results {
        print start_html(-title=>'Word Count 2');

   if (!$file)
   {
        print "<h2>No file uploaded.</h2>";
        return;
   }#END IF

   print h2('File name'),$file;
   print h2('File MIME type'),
   uploadInfo($file)->{'Content-Type'};

my $dashes = "----------------------------------------\n";

        print "<h3>Processed:</h3>";
        print "<h3>Extra Credit Sort by Descending Values</h3>";

        if (param)
        {
                print "<p>$file</p>";
        }
        else
        {
                print "<p>the_raven.txt</p>";
        }

        my $linescount = 0;
        my $word;
        my @totalArray;
        my $totalcount = 0;
        my $unique = 0;
        my $nonunique = 0;
        my $percentage = 0.00;

        while (<$file>)
        {
                chomp(@totalArray = split(/\W+/, "$_"));

                foreach $word (@totalArray)
                {
                        $totalcount++;
                        $word = lc $word;
                        unless (exists $uniqueHash{"$word"} && $uniqueHash{$word} > 0)
                        {
                                $uniqueArray[$unique] = $word;
                                $countsArray[$unique] = 1;
                                $uniqueHash{$uniqueArray[$unique]} = 
$countsArray[$unique];
                                $unique++;
                        }#END UNLESS
                        else
                        {
                                $uniqueArray[$nonunique] = $word;
                                $countsArray[$nonunique] = $uniqueHash{$word} + 1;
                                $uniqueHash{$uniqueArray[$nonunique]} = 
$countsArray[$nonunique];
                                $nonunique++;
                        }#END ELSE
                }#END FOREACH

          $linescount++;
        }#END WHILE
        my @mostArray;
        my $mostword;
        my @mostValues;
        my $mostvalue = 0;
        my $count = 0;
        my $mostpercentage = 0;
        my %mostHash = ();
        @[EMAIL PROTECTED] = @mostValues;

print "<pre>";

                printf "%-15s %-10s %-10s\n", 'Word','Count','Percentage';
                printf "%-35s", $dashes;

  my $i = 0;
        for my $word (sort sort_values keys %uniqueHash)
        {
                $percentage = $uniqueHash{$word}/($totalcount);
                printf "%-15s %5d %5.3f \%\n", "$word", $uniqueHash{$word}, 
$percentage*100;
                if ($uniqueHash{$word} >= $mostvalue)
                {
                        $mostvalue = $uniqueHash{$word};
                        $mostArray[$count] = $word;
                        $mostValues[$count] = $uniqueHash{$word};
                        $mostHash{$mostArray[$count]} = $mostValues[$count];
                        $count++;
                }

        }#END FOREACH
     printf "%-35s", $dashes;
                printf "%-30s %-5d\n", "Total Number of Lines: ", $linescount; #ck
                printf "%-30s %-5d\n", "Total Number of Words: ", $totalcount; #ck
                printf "%-30s %-5d\n", "Total Number of Unique Words:", $unique;

$i = 0;

        for $mostword (sort keys %mostHash)
        {
                ++$i;
                printf "%-30s %-4s\n", "Most Used Word $i: ", "$mostword";
                printf "%-30s %-5d\n", "Most Used Word $i Count: ", 
$mostHash{$mostword};

$mostpercentage = ($mostHash{$mostword}/$totalcount)*100;

printf "%-30s %-5.3f \%\n", "Most Used Word $i Percentage: ", $mostpercentage;
}
printf "%-35s", $dashes;


        print "</pre>"; #check for matching tag
        print end_html;

}#END SUB
exit(0);
#END PROGRAM



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to