I'm guessing the sentence templates in $sentence_file include some sort of marker to indicate where numbers should be inserted, right? Right now as near as I can tell you're using '_5' as a placeholder; you might want to change that to something like #5#. This makes the format more forward-compatible; you can regexp for something like /#\d+#/ and use any number of numbers.

Also: any time you see variables like $second_number, there's bound to be a better way. In this case, you probably want to make an array of your numbers, and join them. Something like:

# If I were doing this as part of a production piece, and # I figured it would be reused and extended and so forth, # I'd probably write a little sub to spit out a comma- # separated list of integers, and regex it into the # original sentence. But I don't think you need that # complexity. if (working_sentence =~ /#\d+#/) { # now we take an Array Slice* from the array of numbers # we got from file. We use $1 from the preceding regex # because... well, because we can. my @working_numbers = @numbers[0..$1-1]; # remove that slice from the numbers array delete @numbers[0..$1-1]; # join the numbers into a comma-separated list my $number_list = join ', ', @working_numbers; # and insert it into the original sentence $working_sentence =~ /#\d+/$number_list/; }

The nice thing about doing it this way is that you can vary the number of numbers you want, and even have several lists in one sentence if you convert this to a while loop. For added points convert this to use int rand and eliminate the need for a numbers file.

* Array slice: you can specify a range within an array, and return it as another array. For more information on slices try perldoc.com.

Note: This code is all untested and stuff, so there may be glaring mistakes. Be warned.

LAI

__END__

In reply to Re: sentence generation by LAI
in thread sentence generation by Ella

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.