Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] IF and strings...

Expand Messages
  • Victor
    Thank you so much. Im now see I forgot to say what wen t wrong. Sorry about that, put your help made it function and thankfully I came to the part in the book
    Message 1 of 2 , Apr 4, 2001
    • 0 Attachment
      Thank you so much. Im now see I forgot to say what wen't wrong.
      Sorry about that, put your help made it function and thankfully I
      came to the part in the book im reading that explains pattern
      matching.

      /Virre

      --- In perl-beginner@y..., "Charles K. Clarkson"
      <c_clarkson@h...> wrote:
      > Victor <virre@v...> wrote:
      > Hi,
      >
      > : Im pretty new to Perl and also use it and try to learn it
      > : on three diffrent systems. You know on a webserver,
      > : on my home PC and on the work Mac. And I am
      > : haveing trouble, I remebered a program that was if I
      > : don't completly miss remember in a book called
      > : "C=64 Tips and Tricks" so it's old C=64 program.
      > : That has a discusion with you, we'll actualy ask
      > : about your name and how old you are and then ask
      > : if you want to play a game with it. The trouble is
      > : when I get to the game if thing. At the moment my
      > : code looks like this:
      > :
      > : ---CODE START---
      > : if($spel != /i'yes'/i or /i'no'/i ) { #checks if $spel not equals
      "yes"
      > =
      > :
      > : # or "no" or 'Yes" or 'No'
      > : print "Yes or No, not $spel"; #if $spel is not yes, no, Yes, No
      =
      > :
      > : #then it prints "Yes or No, not $spel"
      > : }
      > : if($spel != 'yes' or 'Yes') { #checks if $spel is not equal to 'yes'
      or
      > : #'Yes'
      > : print "You don't want to play with me so I don't want you todo
      > : anyting"; #if it does this text is printed
      > : exit 0; #then the programs exit with return value 0.
      > : }
      > :
      > : # Here comes the code if yes was answerd.
      > : ---CODE END---
      >
      > Er, you didn't get around to asking a question, but I think
      > you want to know how to test $spel for a yes or no
      > response. You have a few of choices for the logic. At first
      > glance 'if' looks like it will fill the need, but let's take
      > another look:
      >
      > First we state the problem:
      > 1 - Ask the user a question and get an answer
      > 2 - If the answer is invalid goto 1
      > 3 - If the answer is valid test for yes or no
      > 4 - if 'no' say 'blah blah balh . . .'
      > 5 - if 'yes' continue
      >
      > Second we look at how we might do this in perl:
      > 1 - while validity is false {
      > 2 - Ask question and get answer
      > 3 - If answer is valid exit the while block
      > 4 - test for no
      > 5 - test for yes or just assume yes
      >
      > How do we do the test for validity?
      > We're looking for either of 2 answers (yes or no).
      > Since we're using perl and regular expressions (regexes)
      > are fun, let's use regexes to solve our problem.
      > I'm going to skip a little here, look at perlre for more
      > detail. To search for yes we need a regex like /^yes$/i.
      > To look for no we need /^no$/i. To test for either one
      > we need to use !~, alternation (|), and grouping:
      > (I'll explain below.)
      > _____________________
      > my $spel = '';
      > while ( $spel !~ /^(?:yes|no)$/i ) {
      > # ask user a question and put the
      > # answer in $spel
      > }
      > # $spel contains either yes or no in any case.
      > if ( lc($spel) eq 'no' ) {
      > print "You don't want to play with me so I don't",
      > " want you to do anything.";
      > exit 0; # exit with a return value 0.
      > }
      > # answer was yes
      > ______________________
      > So what does $spel !~ /^(?:yes|no)$/i mean?
      >
      > $spel
      > contains the user input
      >
      > !~
      > a binding operator. It binds $spel to the pattern
      > match. The match is successful if it does *not*
      > match /^(?:yes|no)$/i. The other binding operator
      > is =~
      >
      > /
      > a delimeter. Short for m/. Any character can be
      > used as a delimeter, as long as all delimeters match.
      > The single quote (') has a special meaning, see
      > perlre for details.
      >
      > ^
      > an anchor. This one matches the beginning of the
      > string.
      >
      > (?:
      > a regular expression extension for grouping. This
      > allows us to group 'yes' and 'no' without triggering
      > backreferences like () does.
      >
      > yes|no
      > what we are looking for. The vertical bar '|' is for
      > alternation. You could read it as 'yes or no'.
      >
      > )
      > end of grouping.
      >
      > $
      > an anchor. This one matches the end of the string.
      >
      > /
      > a delimeter. All delimeters should match.
      >
      > i
      > a modifier to do case-insensitive matching.
      >
      >
      > HTH,
      > Charles K. Clarkson
      >
      > It is one thing to show a man that he is in error,
      > and another to put him in possession of truth.
      > - John Locke, 1690
    Your message has been successfully submitted and would be delivered to recipients shortly.