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

Re: [PBML] dumb question

Expand Messages
  • Charles K. Clarkson
    ... Nothing. Your question isn t all that tricky. Even if you add the ; to the end of the first statement and correct the bareword hem in the third
    Message 1 of 1 , Jun 5, 2001
      j y <y_jim542000@...> writes:

      : $_ = 'Us ? The bus usually waits for us, unless the
      : driver forgets us.'
      : print "$_\n";
      : s/\b([Uu]) s(\W) /chr(ord($1)-1).hem.$2/eg;
      : print "$_\n";
      :
      :
      : My question:
      :
      : What would be $1 and $2 in above?.
      :

      Nothing. Your question isn't all that tricky.
      Even if you add the ';' to the end of the first
      statement and correct the bareword 'hem' in the
      third statement, the regex doesn't match. So $1
      and $2 were never defined.

      Assuming this was a serious question and not
      a trick one; Let's take a closer look at that
      regular expression:

      s/\b([Uu]) s(\W) /chr(ord($1)-1).hem.$2/eg;

      s/ # begin substitution regex
      \b # match on word boundary
      ( # begin grouping for $1
      [Uu] # character class for U or u
      ) # end grouping for $1
      # match on a single space character
      s # match on 's'
      ( # begin grouping for $2
      \W # match a non-word character
      ) # end grouping for $1
      # match on a single space character
      / # end RHS of substitution regex

      So we're trying to find 'U s' or 'u s' followed
      by a nonword character followed by a space.

      chr(ord($1)-1).hem.$2 won't work normally.

      try testing with warnings turned on:

      my $first = u;
      my $second = '?';
      my $result = chr(ord($first) - 1) . hem . $second;
      print $result;

      'hem' needs to be in single quotes.

      I assume you were curious and just wanted to
      know what a passage in an article or magazine
      meant. If those were not typos on your part,
      don't use that source for perl info in the
      future.
      If you were just trying to test this code
      and weren't close to a system that could test
      perl, at least have the courtesy to tell us.
      Perl is best at testing perl programs, not
      humans.
      If you're trying to accomplish something,
      let us know what it is and we will jump at the
      chance to help.

      Now in the following, $1 and $2 are
      filled three times. I replaced the g
      modifier with a while loop. (which does
      the same thing.):

      $_ = 'Us? The bus usually waits for us,
      unless the driver forgets us.';
      print "$_\n";

      while (s/\b([Uu]) s(\W) /
      chr(ord($1) - 1) . 'hem' . $2/ex) {;
      print "$_\n";
      print "1 - '$1',\t 2 - '$2'\n";
      }

      prints:
      Us? The bus usually waits for us,
      unless the driver forgets us.
      Them? The bus usually waits for us,
      unless the driver forgets us.
      1 - 'U', 2 - '?'
      Them? The bus usually waits for them,
      unless the driver forgets us.
      1 - 'u', 2 - ','
      Them? The bus usually waits for them,
      unless the driver forgets them.
      1 - 'u', 2 - '.'

      HTH,
      Charles K. Clarkson


      People who mean well - Don't!
    Your message has been successfully submitted and would be delivered to recipients shortly.