in reply to Re: chapters,sentences, and arrays
in thread chapters,sentences, and arrays
construct a lookup hash mapping sentences to chapters
The keys of this hash turn out to be the consecutive numbers from one up to the total number of sentences. The lookup could just as well be an array.
my @chapters = ( 15, 24, 23, 110, 30, 58, 3, 22, 79, 6 ); my $chapter = 1; my @sentenceToChapter = ( undef, # start counting at 1 map { ($chapter++) x $_ } @chapters, ); print qq{Sentence $_ is in chapter $sentenceToChapter[ $_ ]\n} for qw{ 250 15 16 98 };
|
|---|