Find the answer to your Linux question:
Results 1 to 2 of 2
I am trying to make a random sentence generator in perl. So far I can loop it x times to make a fixed quantity of words, but I don't want ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Location
    uk
    Posts
    20

    perl match character

    I am trying to make a random sentence generator in perl.
    So far I can loop it x times to make a fixed quantity of words, but I don't want to do that.
    Or I can let it go on until I hit ctrl-C :/

    I want to have it so that when it reaches a word with a sentence terminating punctuation mark it stops.

    My attempt to do that was with:
    until ($last =~ m/[!\.\?]$/) {
    <generate words for sentence>
    }

    This is doing the woooosh text until ctrl-C thing...

    Sooooooo.... how can I do it right?

    EDIT: Ok, that was actually working, the problem was something else.
    Now however I am not sure how I can cut off everything after the punctuation mark (when it exists).

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I don't understand your question. This should work:
    Code:
    while(1)
    {
        # generate text and store each line in $last
        last if $last =~ /[.?!]$/;
    }
    This way, the last line will be checked against a regular expression that matches a period, question mark, or exclamation point at the end. If the line matches, we exit the loop.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...