Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi
if i have a book which contains 10 chapters and my purpose is to give every sentence in the book a number from 1 to 370 which is the total number of sentences
suppose the number of sentences for every chapter like this:
15, 24, 23, 110, 30, 58, 3, 22, 79, 6
the accumulating number of sentences is
15 39 62 172 202 260 263 285 364 370
the purpose of the program is to tell for example if i want sentence number 250 then it is in chapter 6 , i have the following solution, are there other solutions please!.
use warnings; use strict; my $counter=0; my $tot=0; my $wantedSentence; my $item; my @b=(0,0,0,0,0,0,0,0,0,0); my @a=(15,24,23,110,30,58,3,22,79,6); foreach my $numOfSentences (@a) { $tot=$tot + $numOfSentences; #accumulating totals to @b; $b[$counter]=$tot; $counter++; } print "@b\n"; $counter=0; $wantedSentence = 250; foreach $item (@b) { if($wantedSentence <= $b[$counter]) {$counter++;print "this sentence in chapter $counter";last;} $counter++; }

Replies are listed 'Best First'.
Re: chapters,sentences, and arrays
by brsaravan (Scribe) on Oct 21, 2008 at 10:45 UTC
    This is working for me.
    my @a=(15,24,23,110,30,58,3,22,79,6); my ($x, $cnt) = 0; my $wantedSentence= 250; map {$x += $_; ++$cnt unless($x >= $wantedSentence)}@a; print "This sentence in chapter " . ++$cnt;
Re: chapters,sentences, and arrays
by johngg (Canon) on Oct 21, 2008 at 14:11 UTC
    If you are going to be looking for multiple sentences it might pay off to construct a lookup hash mapping sentences to chapters.

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

    The output.

    Sentence 250 is in chapter 6 Sentence 15 is in chapter 1 Sentence 16 is in chapter 2 Sentence 98 is in chapter 4

    I hope this is useful.

    Cheers,

    JohnGG

      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 };
Re: chapters,sentences, and arrays
by luckypower (Beadle) on Oct 21, 2008 at 10:21 UTC
    hi, you can do this using just a single array.
    my @a=(15,24,23,110,30,58,3,22,79,6); my $wantedSentence = 250; my $chptr = 1; foreach (@a) { $wantedSentence -= $_; if ($wantedSentence <= 0) { print "$chptr"; last; } $chptr++; }
Re: chapters,sentences, and arrays
by Limbic~Region (Chancellor) on Oct 21, 2008 at 15:00 UTC
    Anonymous Monk,
    This could be an interesting math problem if you wanted it to be. The other solutions provided are more practical and if they meet your needs, you can stop reading now.

    In essence, you are trying to determine a hidden property of a number (which chapter a sentence is in) by looking at some other property of the number (its ordinal value in all sentences). Are you looking for a way to create a function that given any sentence in the book, determine which chapter it is in by some creative math approach? If so, please respond. If not, probably the most efficient way is to map all sentences to a chapter in the book using a hash (assumes book is static, numerous lookups, and that memory consumption is not prohibitive).

    Cheers - L~R

      In case we're voting, I agree with Anonymous Monk that I would be interested in a creative-math approach to finding the chapter of a given book containing a given sentence.
        JadeNB,
        In a nutshell, this problem boils down to where this number fits in a list. The obvious non-creative solution to this is a binary search because it is both space and time efficient.

        It would also be fairly trivial if each chapter had the same number of sentences. One trick might be then to find the chapter with the most number of sentences and pad each chapter to be equal to that chapter. When you "hand out" sentences and their ordinal value, there will be gaps representing the padding but you can then instantly take any sentence and find which chapter it belongs to. You can also find its true overall ordinal value in the book as well as the ordinal value in the chapter. Consider the following:

        Chapter 1: 3 sentences Chapter 2: 5 sentences Chapter 3: 4 sentences Chapter 4: 6 sentences The chapter with the longest chapter is 4, so we pad all of them to 6 Chapter 1: 01, 02, 03 # 04, 05, 06 are padded real: 01, 02, 03 Chapter 2: 07, 08, 09, 10, 11 # 12 is padded real: 04, 05, 06, 07, 08 Chapter 3: 13, 14, 15, 16 # 17 and 18 are padded real: 09, 10, 11, 12 Chapter 4: 19, 20, 21, 22, 23, 24 # no padding real: 13, 14, 15, 16, 17, 18 Now let's say someone says, what chapter does 15 belong to? ceil(15 / 6) = 3 Now let's say someone wants to know the true ordinal overall value my %pad = (1 => 0, 2 => 3, 3 => 4, 4 => 6); 15 - $pad{ceil(15 / 6)} = 11 Now let's say someone wants to know what is the ordinal value of the s +entence within the chapter 15 mod 6 = 3

        Cheers - L~R

Re: chapters,sentences, and arrays
by Anonymous Monk on Oct 21, 2008 at 16:08 UTC
    thanks for the answers
    Limbic~Region : indeed it is a fun project i have envisaged while learning perl since i am a beginner, and i have tried to solve this problem in visual basic 6 , but found that coding in perl is easier
    your suggestion about creating a function that given any sentence in the book, determine which chapter it is in by some creative math approach, is interesting and deserve investigation, we can envisaged a story author who may arrange his book like this. but for a random chosen book is it possible to create a magical formula which accomodate the random number of sentences in the chapters?, in this case we may need a statistical analysis to determine if this is possible.