home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5817 < prev    next >
Encoding:
Text File  |  1992-09-09  |  2.7 KB  |  87 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gatech!darwin.sura.net!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Case dependent substitution
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Sep9.235921.13124@news.eng.convex.com>
  8. Date: Wed, 9 Sep 1992 23:59:21 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <1992Sep9.191433.25724@uvaarpa.Virginia.EDU>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 69
  17.  
  18. From the keyboard of noran!iowa!kburton@uunet.uu.net:
  19. :
  20. :I would like to substitute an arbitrary pattern for another, but it all of
  21. :the alphabetic characters in the source pattern are upper case I would like
  22. :to case the replacement pattern to also force upper case substitution.
  23. :
  24. :For example:
  25. :
  26. :    substitute "abc" in "ABCxyzabc" with "xyz" produces "XYZxyzxyz"
  27. :    substitute "abc" in "abcxyzABC" with "xyz" produces "xyzxyzXYZ"
  28. :
  29. :I have started to work on a solution that first searches the string for pattern
  30. :matches, checks the mathched patten, then finally makes the appropriate
  31. :substitution. This "brute force" method doesn't seem very elegant or efficient
  32. :and I was wondering if any of the more experienced perl users could give me
  33. :a tip ? 
  34.  
  35. I've often wanted a /I switch that behaved this way.  Here's
  36. what I use:
  37.  
  38.     s/abc/&mapcase('xyz')/gie;
  39.  
  40. where mapcase is as follows:
  41.  
  42.     sub mapcase {
  43.     local($lhs, $rhs) = ($&, shift);
  44.     for (local($i) = 0; $i < length($lhs); $i++) {
  45.         substr($lhs, $i, 1) =~ tr/A-Z//
  46.         ? substr($rhs, $i, 1) =~ tr/a-z/A-Z/
  47.         : substr($rhs, $i, 1) =~ tr/A-Z/a-z/;
  48.     }
  49.     $rhs;
  50.     }
  51.  
  52. You could do better if you hand-rolled it, because you could
  53. save how the mapping works and not check the lhs each time 
  54. again and again.
  55.  
  56. But it's not entirely satisfactory.  Consider
  57.  
  58.     $_ = "Green is the green apple who has GREEN WORMS\n";
  59.     print;
  60.     s/green/&mapcase3('red')/gie;
  61.     print;
  62.  
  63.     print "\n";
  64.  
  65.     $_ = "Red is the red apple who has RED WORMS\n";
  66.     print;
  67.     s/red/&mapcase3('green')/gie;
  68.     print;
  69.  
  70.  
  71. Yields:
  72.  
  73.     Green is the green apple who has GREEN WORMS
  74.     Red is the red apple who has RED WORMS
  75.  
  76.     Red is the red apple who has RED WORMS
  77.     Green is the green apple who has GREen WORMS
  78.  
  79. Hmm.   I don't thing GREen is what you want here.
  80.  
  81. --tom
  82. -- 
  83.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  84.  
  85. If you want your program to be readable, consider supplying the argument.
  86.             --Larry Wall in the perl man page 
  87.