home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!mips!sdd.hp.com!uakari.primate.wisc.edu!ames!olivea!sgigate!odin!stantz
- From: stantz@sgi.com (Mark Stantz)
- Newsgroups: comp.lang.perl
- Subject: Re: Avoiding intermediate variable
- Message-ID: <1992Aug21.003314.13443@odin.corp.sgi.com>
- Date: 21 Aug 92 00:33:14 GMT
- References: <MARK.MCINTOSH.92Aug20145545@sombrio.UVic.CA>
- Sender: news@odin.corp.sgi.com (Net News)
- Organization: Silicon Graphics, Inc.
- Lines: 27
- Nntp-Posting-Host: sierra.corp.sgi.com
-
- In article <MARK.MCINTOSH.92Aug20145545@sombrio.UVic.CA> Mark.McIntosh@engr.UVic.CA (Mark McIntosh) writes:
- >Someone out my location would like to avoid the intermediate variable
- >in the following piece of code, assuming $y has been set to some value:
- >
- > $tmp = $y;
- > $tmp =~ s/xy/zz/;
- > print $tmp;
- >
- >He wants to retain the value of $y for later use but use a modified
- >version of it once - he won't need it later. Is there a way to do
- >this in Perl without using an intermediate variable?
-
- Don't know whether or not it can be done without a temporary
- variable or not, but
-
- print ((($junk = $y) =~ s/xy/zz/) ? $junk : $y);
-
- should do the job. You need those outermost parentheses, BTW.
-
- Of course, if you're sure the substitution will succeed, you can
- get away with:
-
- print $junk if (($junk = $y) =~ s/xy/zz/);
-
- which is marginally easier to read.
-
- -mfs
-