Cannot understand this fragment of code
- From: giuseppegallone@xxxxxxxxx (Giuseppe.G.)
- Date: Thu, 29 Nov 2007 03:08:30 -0800 (PST)
Hello there, I'm trying to understand and modify some perl code to
create an index of word. The subroutine I have is in OO Perl, and I'm
just starting to learn normal Perl. So I'd like to transform it. Here
it is:
sub make_word_list {
my ( $self ) = @_;
my %all_words;
foreach my $doc ( @{ $self->{docs} } ) {
my %words = $self->get_words( $doc );
foreach my $k ( keys %words ) {
#print "Word: $k\n";
$all_words{$k} += $words{$k};
}
}
#-------------?? from here
# create a lookup hash of word to position
my %lookup;
my @sorted_words = sort keys %all_words;
@lookup{@sorted_words} = (1..$#sorted_words );
$self->{'word_index'} = \%lookup;
$self->{'word_list'} = \@sorted_words;
$self->{'word_count'} = scalar @sorted_words;
}
and it's called by
make_word_list();
Ok, the first part is clear (til when %all_words is created).
%all_words for us is a hash like
KEY VALUE
word frequency in documents
foo 32
cat 12
....
but what about the rest? @sorted_words contains just the sorted words
with no frequency, and then?
Thank you so much
Giuseppe
.
- Follow-Ups:
- Re: Cannot understand this fragment of code
- From: John W . Krahn
- Re: Cannot understand this fragment of code
- Prev by Date: Re: How can I sort files by timestamp without slurping?
- Next by Date: Re: extracting fields from a matching line
- Previous by thread: Usage: DCE::RPC::new (new DCE::RPC, $auth) at
- Next by thread: Re: Cannot understand this fragment of code
- Index(es):
Relevant Pages
|