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

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!mips!sdd.hp.com!uakari.primate.wisc.edu!ames!olivea!sgigate!odin!stantz
  2. From: stantz@sgi.com (Mark Stantz)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Avoiding intermediate variable
  5. Message-ID: <1992Aug21.003314.13443@odin.corp.sgi.com>
  6. Date: 21 Aug 92 00:33:14 GMT
  7. References: <MARK.MCINTOSH.92Aug20145545@sombrio.UVic.CA>
  8. Sender: news@odin.corp.sgi.com (Net News)
  9. Organization: Silicon Graphics, Inc.
  10. Lines: 27
  11. Nntp-Posting-Host: sierra.corp.sgi.com
  12.  
  13. In article <MARK.MCINTOSH.92Aug20145545@sombrio.UVic.CA> Mark.McIntosh@engr.UVic.CA (Mark  McIntosh) writes:
  14. >Someone out my location would like to avoid the intermediate variable
  15. >in the following piece of code, assuming $y has been set to some value:
  16. >
  17. >       $tmp = $y;
  18. >       $tmp =~ s/xy/zz/;
  19. >       print $tmp;
  20. >
  21. >He wants to retain the value of $y for later use but use a modified
  22. >version of it once - he won't need it later.  Is there a way to do
  23. >this in Perl without using an intermediate variable?
  24.  
  25.     Don't know whether or not it can be done without a temporary 
  26. variable or not, but
  27.  
  28.         print ((($junk = $y) =~ s/xy/zz/) ? $junk : $y);
  29.  
  30.     should do the job.  You need those outermost parentheses, BTW.
  31.  
  32.     Of course, if you're sure the substitution will succeed, you can
  33. get away with:
  34.  
  35.         print $junk if (($junk = $y) =~ s/xy/zz/);
  36.  
  37.     which is marginally easier to read.  
  38.  
  39.     -mfs
  40.