Re: Cannot understand this fragment of code



On Thursday 29 November 2007 03:08, Giuseppe.G. wrote:

Hello there,

Hello,

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

@lookup{ } is a hash slice. Search for "Slices" in the perldata man
page:

perldoc perldata


Also, if you have warnings enabled, you should get a warnings when that
line is run. Say that @sorted_words contains four elements therefore
the value of $#sorted_words will be 3 and the message "Odd number of
elements in hash assignment" should be displayed. That line should be:

@lookup{ @sorted_words } = 1 .. @sorted_words;

Or:

@lookup{ @sorted_words } = 0 .. $#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?

That depends on what the rest of the program is doing.



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: Noob! Help required and would be really appreciated!
    ... You've read 60% of "Learning Perl"? ... On finding the keyword "New Order", it has to start building a hash. ... want to build a loop around this hash creation. ... transferred to %allOrders. ...
    (comp.lang.perl.misc)
  • Re: Passing an array to a sub routine
    ... but later you're using a hash. ... In Perl, these are very ... The LoadDataFile routine works correctly, ...
    (comp.lang.perl.misc)
  • Making Datatypes Constant and Emulating Const Correctness
    ... Being relatively new to Perl with Java and bits of C under my belt, ... I'm new to C+++, too, but remember a number of uses for the const keyword ... an array to some analizer function as a constant strongly signals that no ... some reference (e,g. a blessed hash reference) ...
    (comp.lang.perl.misc)
  • Re: taking references to functions
    ... > I'm building a diagnostic medical questionnaire using Perl Tk. ... > to create a reference to a named function but not for unnamed ones. ... Notice that %dispatch here is an actual hash variable. ...
    (perl.beginners)
  • Re: arrange form data in same order as on form
    ... In Perl, f you get your submitted name/value pairs from the ... module as a "hash", then of course the ordering has been lost by then. ... the element, now deprecated, from earlier versions of HTML). ... their scripts were easier to write, ...
    (comp.lang.perl.misc)