in reply to sentence generation
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__
|
|---|