home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5712 < prev    next >
Encoding:
Text File  |  1992-09-03  |  2.4 KB  |  68 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!cs.utexas.edu!hellgate.utah.edu!lanl!newshost.lanl.gov!jgd
  3. From: jgd@acl.lanl.gov (Jerry G. DeLapp)
  4. Subject: Re: Avoiding intermediate variable
  5. In-Reply-To: stantz@sgi.com's message of 21 Aug 92 00:33:14 GMT
  6. Message-ID: <JGD.92Sep3145140@kodiak.acl.lanl.gov>
  7. Sender: news@newshost.lanl.gov
  8. Reply-To: jgd@acl.lanl.gov
  9. Organization: Advanced Computing Lab, LANL, NM
  10. References: <MARK.MCINTOSH.92Aug20145545@sombrio.UVic.CA>
  11.     <1992Aug21.003314.13443@odin.corp.sgi.com>
  12. Date: Thu, 3 Sep 1992 21:51:40 GMT
  13. Lines: 53
  14.  
  15. >>>>> On 21 Aug 92 00:33:14 GMT, stantz@sgi.com (Mark Stantz) said:
  16. Mark> Nntp-Posting-Host: sierra.corp.sgi.com
  17.  
  18. Mark> In article <MARK.MCINTOSH.92Aug20145545@sombrio.UVic.CA> Mark.McIntosh@engr.UVic.CA (Mark  McIntosh) writes:
  19. >Someone out my location would like to avoid the intermediate variable
  20. >in the following piece of code, assuming $y has been set to some value:
  21. >
  22. >       $tmp = $y;
  23. >       $tmp =~ s/xy/zz/;
  24. >       print $tmp;
  25. >
  26. >He wants to retain the value of $y for later use but use a modified
  27. >version of it once - he won't need it later.  Is there a way to do
  28. >this in Perl without using an intermediate variable?
  29.  
  30. Mark>         print ((($junk = $y) =~ s/xy/zz/) ? $junk : $y);
  31.  
  32. Mark>     should do the job.  You need those outermost parentheses, BTW.
  33.  
  34. Mark>         print $junk if (($junk = $y) =~ s/xy/zz/);
  35.  
  36. Well, you can do it with an automagically generated variable a'la:
  37.  
  38.     sub foo {
  39.         print ($_[0],zz,$_[1]);
  40.     }
  41.     $y='gortxyflort';
  42.     &foo($y=~/(.*)xy(.*)/);
  43.     exit;
  44.  
  45. or:
  46.  
  47.     $y='gortxyflub';
  48.     $y=~/(.*)xy(.*)/;
  49.     print $1,zz,$2;
  50.     exit;
  51.  
  52. There's still an intermediate variable used here, but I'm assuming the user
  53. objects to having to "invent" his own temporary variable. I've used the first
  54. trick extensively in a package here where the first character of a pattern is
  55. an optional single character switch, and the remainder is a pattern. Whenever
  56. I call a subroutine which doesn't care about the switch, I put a pattern like
  57.  
  58. $var=~/^[abq](.*)/
  59.  
  60. in the argument list, and voila, the pattern is handed into the subroutine
  61. sans the switch character, without destroying $var. This makes it easier to
  62. code the subroutine, because I can concentrate on the data value I want to
  63. manipulate, instead of worrying about the optional baggage.
  64. --
  65.  
  66. Jerry G. DeLapp -- ACL System Scientist -- (505) 665-4531 <jgd@lanl.gov>
  67. Computing beyond the Bleeding Edge -- The Advanced Computing Laboratory
  68.