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

substitution + backreferences + function call

Expand Messages
  • da5id
    Hey all, Just wondering syntactically how to use a function within the substitution operator. I m essentially iterating through a series of keywords and trying
    Message 1 of 3 , Apr 15 6:06 AM
    • 0 Attachment
      Hey all,

      Just wondering syntactically how to use a function within the
      substitution operator. I'm essentially iterating through a series of
      keywords and trying to uppercase them:

      my @keywords = qw(select from where in between having group order by
      left right join);
      $_ = $sql;
      for my $keyword(@keywords){
      s/\b($keyword)\b/$1/gi;
      }
      $sql = $_;

      but when I try to use:
      s/\b($keyword)\b/uc $1/gi;

      I'm getting a literal "uc" rather than the results of the function. Any
      suggestions - is my approach a sound one?
    • merlyn@stonehenge.com
      ... da5id but when I try to use: da5id s/ b($keyword) b/uc $1/gi; da5id I m getting a literal uc rather than the results of the function. Any da5id
      Message 2 of 3 , Apr 15 7:02 AM
      • 0 Attachment
        >>>>> "da5id" == da5id <da5id@...> writes:

        da5id> but when I try to use:
        da5id> s/\b($keyword)\b/uc $1/gi;

        da5id> I'm getting a literal "uc" rather than the results of the function. Any
        da5id> suggestions - is my approach a sound one?

        Well, for "uc" specifically, you can use \U:

        s/\b($keyword)\b/\U$1/gi;

        But in general, you can use /e, as in:

        s/\b($keyword)\b/uc $1/gie;

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      • da5id
        ... Very cool - I like the option of using e as a regex evaluation option since I can envision myself needing to evaluate a lot as I do the substitution. I
        Message 3 of 3 , Apr 15 7:15 PM
        • 0 Attachment
          > you can use \U: s/\b($keyword)\b/\U$1/gi;

          Very cool - I like the option of using e as a regex evaluation option
          since I can envision myself needing to evaluate a lot as I do the
          substitution. I looked in the Llama book and didn't see this but then
          found a reference in Freidl's Mastering Regular Expressions although it
          missed a complete list. He called it a Case-Folding Span. Are \1 and \U
          the only ones of these?

          Thanks much,

          David


          Randal L. Schwartz wrote:
          >>>>>> "da5id" == da5id <da5id@...> writes:
          >>>>>>
          >
          > da5id> but when I try to use:
          > da5id> s/\b($keyword)\b/uc $1/gi;
          >
          > da5id> I'm getting a literal "uc" rather than the results of the function. Any
          > da5id> suggestions - is my approach a sound one?
          >
          > Well, for "uc" specifically, you can use \U:
          >
          > s/\b($keyword)\b/\U$1/gi;
          >
          > But in general, you can use /e, as in:
          >
          > s/\b($keyword)\b/uc $1/gie;
          >
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.