Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: sentence generation

by LAI (Hermit)
on Jul 23, 2003 at 13:27 UTC ( [id://277138]=note: print w/replies, xml ) Need Help??


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__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://277138]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found