Teresa Raymond <[EMAIL PROTECTED]> wrote:
: 
: 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.

    You may be timing out. That is your program may be
taking too long to parse a larger file and the browser
"thinks" the connection is down.

    Add:
$|++;

    near the top of your script. It will change the way
buffering works. Read "perlvar" for more info.


    You have a lot unnecessary code. For example,
@countsArray and @uniqueArray are not needed. Here's the
block of code they are used in:

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

    First, we eliminate the @countsArray:

foreach my $word (@totalArray) {
    $totalcount++;
    $word = lc $word;
    unless (exists $uniqueHash{$word} && $uniqueHash{$word} > 0) {
        $uniqueArray[$unique] = $word;

        #$countsArray[$unique] = 1;
        #$uniqueHash{$uniqueArray[$unique]} = $countsArray[$unique];

        $uniqueHash{$uniqueArray[$unique]} = 1;
        $unique++;

    } else {
        $uniqueArray[$nonunique] = $word;

        #$countsArray[$nonunique] = $uniqueHash{$word} + 1;
        #$uniqueHash{$uniqueArray[$nonunique]} = $countsArray[$nonunique];

        $uniqueHash{$uniqueArray[$nonunique]} = $uniqueHash{$word} + 1;
        $nonunique++;
    }
}

   Then @uniqueArray:

foreach my $word (@totalArray) {
    $totalcount++;
    $word = lc $word;
    unless (exists $uniqueHash{$word} && $uniqueHash{$word} > 0) {
        #$uniqueArray[$unique] = $word;

        #$countsArray[$unique] = 1;
        #$uniqueHash{$uniqueArray[$unique]} = $countsArray[$unique];

        $uniqueHash{$word} = 1;
        #$unique++;

    } else {
        #$uniqueArray[$nonunique] = $word;

        #$countsArray[$nonunique] = $uniqueHash{$word} + 1;
        #$uniqueHash{$uniqueArray[$nonunique]} = $countsArray[$nonunique];

        $uniqueHash{$word} = $uniqueHash{$word} + 1;
        #$nonunique++;
    }
}

    Next, we clean it up a bit and get rid of the test:

foreach my $word (@totalArray) {
    $totalcount++;
    $word = lc $word;
    $uniqueHash{$word}++;
}

    Here's everything with better scoped variables:

my %uniqueHash;
my $totalcount = 0;
while (<$file>) {
    chomp( my @totalArray = split /\W+/ );

    foreach my $word (@totalArray) {
        $uniqueHash{lc $word}++;
    }
    $totalcount += @totalArray;
}
my $linescount = $.;



    I assume the other arrays were used for testing. You
need to look through the rest of your code to see what
else can be cut. I didn't look through all of it, but as
others have pointed out, declaring variables outside the
smallest scope possible leads to problems in large
scripts. Perhaps others might learn from this.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
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