home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5883 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  2.4 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!rpi!uwm.edu!ogicse!orstcs!prism!jacobsd
  2. From: jacobsd@prism.cs.orst.edu (Dana Jacobsen)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Help with variable interpolation inside s/// (repost)
  5. Keywords: variable interpolation, substitution
  6. Message-ID: <1992Sep12.033628.2470@CS.ORST.EDU>
  7. Date: 12 Sep 92 03:36:28 GMT
  8. References: <1992Sep11.222422.24918@CS.ORST.EDU>
  9. Sender: usenet@CS.ORST.EDU
  10. Organization: Oregon State University, Computer Science Dept
  11. Lines: 51
  12. Nntp-Posting-Host: prism.cs.orst.edu
  13.  
  14. In <1992Sep11.222422.24918@CS.ORST.EDU> hanusj@storm.CS.ORST.EDU (Joe Hanus) writes:
  15. >We have written a short capitalization script that's not working. The
  16. >script assumes all words not found in a standard dictionary are proper
  17. >nouns.  Those found in the dictionary are capitalized by replacing
  18. >with the word from the dictionary. 
  19.  
  20. >The line giving the problem is commented
  21. >We are using perl 4.035
  22.  
  23. >Can you pinpoint my problem or suggest a work-around?
  24.  
  25.   While I can't solve the problem (it looks like your code should work
  26. to me), I can suggest a workaround that not only works, it is much faster
  27. than the method you're using.  Hint: loop over the sentence, not the
  28. dictionary.  I also use the "\u" and "\L" operaters defined on page 70 of
  29. the Camel book to do the capitalization.
  30.  
  31. #!/usr/local/bin/perl
  32.  
  33. open( CHANGES, "/usr/dict/words" );
  34. while (<CHANGES>) {
  35.   chop;
  36.   $realcap = $_;
  37.   # convert into "Captilized Format"
  38.   s/^(\S)(\S*)/\u$1\L$2/;
  39.   $rwords{$_} = $realcap;
  40. }
  41. close( CHANGES );
  42.  
  43. while ( <> ) {
  44.    s/\b(\S)(\S*)\b/\u$1\L$2/g;
  45.    foreach $word ( split(/\W/) ) {
  46.      s/\b$word\b/$rwords{$word}/g if $rwords{$word};
  47.    }
  48.    print;
  49. }
  50.  
  51. exit 0;
  52.  
  53. >------------------------------------------------------------------------------
  54. >Joe Hanus                            | E-MAIL hanusj@bionette.cgrb.orst.edu
  55. >Microbial Germplasm Database         |
  56. >Dept. of Botany and Plant Pathology  | Phone  (503)-737-5300
  57. >Oregon State University              | FAX    (503)-737-3045
  58. >Corvallis, OR 97331-2902             |
  59. >______________________________________________________________________________
  60. --
  61. Dana Jacobsen                       I that wes in heill and gledness
  62. jacobsd@cs.orst.edu                   am trublit now with gret sikness
  63. jacobsd@solar.cor2.epa.gov              and feblit with infirmetie
  64. Computer Sciences Corporation             Timor mortis conturbat me.
  65.