jjohhn has asked for the wisdom of the Perl Monks concerning the following question:
I am writing a subroutine that chooses a random key from a hash, but the choice is weighted depending on the value of the hash. The keys are a list of words from a text, the values are the frequency of occurence. (This is from the online book "Learning Perl the Hard Way" http://greenteapress.com/perl/)
push @weighted $hash{$key);
to
push @weighted $key;error still there
anyway I don't understand the syntax error being flagged:
update: changed# sub weighted_hash # chooses and returns a random key from a hash; # the probability of choosing a specific key # is weighted by the value associated with that key sub weighted_hash{ my $hashref = shift; my %hash = $$hashref; my @weighted; foreach my $key (keys %hash){ my $weight = $hash{$key}; while ($weight >0){ push @weighted $key; $weight--; } } rand_elt @weighted; }
push @weighted $hash{$key);
to
push @weighted $key;error still there
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: "Scalar found where operator expected", references
by dave_the_m (Monsignor) on May 22, 2005 at 00:24 UTC | |
Re: "Scalar found where operator expected", references
by fglock (Vicar) on May 21, 2005 at 23:31 UTC | |
Re: "Scalar found where operator expected", references
by sh1tn (Priest) on May 22, 2005 at 00:15 UTC | |
Re: "Scalar found where operator expected", references
by Roger (Parson) on May 22, 2005 at 09:21 UTC | |
Re: "Scalar found where operator expected", references (perl6)
by mrborisguy (Hermit) on May 22, 2005 at 16:35 UTC | |
Re: "Scalar found where operator expected", references
by TedPride (Priest) on May 22, 2005 at 16:45 UTC |
Back to
Seekers of Perl Wisdom