home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5406 < prev    next >
Encoding:
Internet Message Format  |  1992-08-20  |  2.0 KB

  1. Path: sparky!uunet!ogicse!uwm.edu!cs.utexas.edu!convex!convex!tchrist
  2. From: tchrist@convex.COM (Tom Christiansen)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Avoiding intermediate variable
  5. Message-ID: <1992Aug21.001412.22695@news.eng.convex.com>
  6. Date: 21 Aug 92 00:14:12 GMT
  7. Article-I.D.: news.1992Aug21.001412.22695
  8. References: <MARK.MCINTOSH.92Aug20145545@sombrio.UVic.CA>
  9. Sender: usenet@news.eng.convex.com (news access account)
  10. Reply-To: tchrist@convex.COM (Tom Christiansen)
  11. Organization: Convex Computer Corporation, Colorado Springs, CO
  12. Lines: 42
  13. Originator: tchrist@pixel.convex.com
  14. Nntp-Posting-Host: pixel.convex.com
  15. X-Disclaimer: This message was written by a user at CONVEX Computer
  16.               Corp. The opinions expressed are those of the user and
  17.               not necessarily those of CONVEX.
  18.  
  19. From the keyboard of Mark.McIntosh@engr.UVic.CA (Mark  McIntosh):
  20. :Someone out my location would like to avoid the intermediate variable
  21. :in the following piece of code, assuming $y has been set to some value:
  22. :
  23. :       $tmp = $y;
  24. :       $tmp =~ s/xy/zz/;
  25. :       print $tmp;
  26.     
  27. :He wants to retain the value of $y for later use but use a modified
  28. :version of it once - he won't need it later.  Is there a way to do
  29. :this in Perl without using an intermediate variable?
  30.  
  31. You can always use
  32.  
  33.     ($tmp = $y) =~ s/xy/zz/;
  34.     print $tmp;
  35.  
  36. But there's still a temporary.  If you tried quite hard, you 
  37. could do 
  38.  
  39.     print $y =~ /xy/ && "$`zz$'";
  40.  
  41. but that seems too obfuscating.
  42.  
  43. :If not, then we suggest having another form of the "=~" operator (or a
  44. :function?) which returns the modified value instead of replacing the
  45. :value in the "operated on" variable.  Maybe a "~" operator.
  46. :
  47. :eg.
  48. :      print $y ~ s/xy/zz/;
  49. :
  50. :Are we crazy?  I think this would be useful.  Or are we missing something?
  51.  
  52. Therein lies the awk fallacy.  Tilde already has a legimate meaning. 
  53. Usurp it not.
  54.  
  55. --tom
  56. -- 
  57.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  58.  
  59.     I dunno, I dream in Perl sometimes...
  60.                     --Larry Wall in  <8538@jpl-devvax.JPL.NASA.GOV>
  61.