Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: sentence generationby LAI (Hermit) |
on Jul 23, 2003 at 13:27 UTC ( [id://277138]=note: print w/replies, xml ) | Need Help?? |
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:
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 Section
Seekers of Perl Wisdom
|
|