use strict; use warnings; my @chapters = ( 15, 24, 23, 110, 30, 58, 3, 22, 79, 6 ); my $base = 1; my %sentenceToChapter = map { my $chapter = $_; my @range = ( $base .. $base + $chapters[ $chapter ] - 1 ); $base += scalar @range; map { $_ => $chapter + 1 } @range; } 0 .. $#chapters; print qq{Sentence $_ is in chapter $sentenceToChapter{ $_ }\n} for qw{ 250 15 16 98 }; #### Sentence 250 is in chapter 6 Sentence 15 is in chapter 1 Sentence 16 is in chapter 2 Sentence 98 is in chapter 4