Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Print modified version of a string w/o intermediate variable or subroutine

Reply
Thread Tools

Print modified version of a string w/o intermediate variable or subroutine

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-07-2005
I have a variable which contains a text string. I want to print a
modified verion of the string without changing the variable. Consider
this code, which simply reverses each occurance of the word "gas":

my $word = 'gas';
my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
(my $temp = $string) =~ s/$word/reverse $word/ieg;
print "$temp\n";

That works, but it uses an intermediate variable ($temp). I dislike
intermediate variables.

I could create a subroutine, but that's even uglier.

Is there an elegant way to print the modified version of the string
without an intermediate variable or a subroutine?

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-07-2005
wrote:
> I have a variable which contains a text string. I want to print a
> modified verion of the string without changing the variable. Consider
> this code, which simply reverses each occurance of the word "gas":
>
> my $word = 'gas';
> my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
> (my $temp = $string) =~ s/$word/reverse $word/ieg;
> print "$temp\n";
>
> That works, but it uses an intermediate variable ($temp). I dislike
> intermediate variables.
>
> I could create a subroutine, but that's even uglier.
>
> Is there an elegant way to print the modified version of the string
> without an intermediate variable or a subroutine?


Depending on your definition of elegant:

$ perl -le'
my $word = q/gas/;
my $string = "Jumping Jack Flash, It\047s a gas, gas, gas.";
print $string;
print map lc eq lc $word ? scalar reverse : $_, split /($word)/i, $string;
'
Jumping Jack Flash, It's a gas, gas, gas.
Jumping Jack Flash, It's a sag, sag, sag.



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-07-2005
>John W. Krahn wrote:
>>> print map lc eq lc $word ? scalar reverse : $_, split /($word)/i, $string;


Clever. I didn't realize you could do something like "split /($word)/".
That's handy to know.

Thanks!

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-08-2005
:

> I have a variable which contains a text string. I want to print a
> modified verion of the string without changing the variable. Consider
> this code, which simply reverses each occurance of the word "gas":
>
> my $word = 'gas';
> my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
> (my $temp = $string) =~ s/$word/reverse $word/ieg;
> print "$temp\n";
>
> That works, but it uses an intermediate variable ($temp). I dislike
> intermediate variables.


How do you feel about using '$_'?

$_ = $string;
s/$word/reverse $word/ieg;
print $_, "\n";

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-08-2005
Dr.Ruud <rvtol+> wrote in comp.lang.perl.misc:
> :
>
> > I have a variable which contains a text string. I want to print a
> > modified verion of the string without changing the variable. Consider
> > this code, which simply reverses each occurance of the word "gas":
> >
> > my $word = 'gas';
> > my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
> > (my $temp = $string) =~ s/$word/reverse $word/ieg;
> > print "$temp\n";
> >
> > That works, but it uses an intermediate variable ($temp). I dislike
> > intermediate variables.

>
> How do you feel about using '$_'?
>
> $_ = $string;
> s/$word/reverse $word/ieg;
> print $_, "\n";


Squeezing it in a one-liner:

print do { s/$word/reverse $word/ieg; $_ }, "\n" for "$string";

The quotes around the bare variable "$string" are needed to protect $string
from being changed in the process. Without them, $_ would be an alias for
$string and s/// would apply to it.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-08-2005
Anno Siegel:

> Squeezing it in a one-liner:
> print do { s/$word/reverse $word/ieg; $_ }, "\n" for "$string";



I had been playing with these:

print do {$_=$string; s/$word/reverse $word/ieg; $_}, "\n";


print eval($_=$string, s/$word/reverse $word/ieg, '$_'), "\n";

print ''.($_=$string, s/$word/reverse $word/ieg, $_)."\n";

print ''.($_=$string, s/$word/reverse $word/ieg)[0], "\n";

As you can see, I dont know how to stop 'print' to be a function.

--
Affijn, Ruud <http://www.pandora.com/?sc=sh770781&cmd=tunermini>

"Gewoon is een tijger."
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-08-2005
Dr.Ruud <rvtol+> wrote:


> print eval($_=$string, s/$word/reverse $word/ieg, '$_'), "\n";
>
> print ''.($_=$string, s/$word/reverse $word/ieg, $_)."\n";
>
> print ''.($_=$string, s/$word/reverse $word/ieg)[0], "\n";
>
> As you can see, I dont know how to stop 'print' to be a function.



I think this post should clear it up:

Message-Id: <>


IOW, simply choose to put parenthesis around print's argument list
when you have a parenthesis following the function name.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-09-2005
Tad McClellan:
> Dr.Ruud:


>> print ''.($_=$string, s/$word/reverse $word/ieg)[0], "\n";
>> As you can see, I dont know how to stop 'print' to be a function.

>
> I think this post should clear it up:
>
> Message-Id:


The Internet standard way to express such a pointer:

<news:>

(see RFC 3986)


> IOW, simply choose to put parenthesis around print's argument list
> when you have a parenthesis following the function name.


Yes, I even knew the FAQ, but I hadn't tried it yet. I had even tried
the "+" but couldn't get it to work.

perldoc -f print says: Also be
careful not to follow the print keyword with a left parenthesis
unless you want the corresponding right parenthesis to termi-
nate the arguments to the print--interpose a "+" or put paren-
theses around all the arguments.

OK, now I tried again. This gives the requested output:

print (($_=$string, s/$word/reverse $word/ieg)[0], "\n");

and this too:

print +($_=$string, s/$word/reverse $word/ieg)[0], "\n";

--
Affijn, Ruud <http://www.pandora.com/?sc=sh770781&cmd=tunermini>

"Gewoon is een tijger."

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      10-10-2005
wrote:

> my $word = 'gas';
> my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
> (my $temp = $string) =~ s/$word/reverse $word/ieg;
> print "$temp\n";


> I could create a subroutine, but that's even uglier.
>
> Is there an elegant way to print the modified version of the string
> without an intermediate variable or a subroutine?


That depends on what is you objection to a subroutine.

If you just want to your code to look smooth and you are not worrying
about the subroutine call overhead you could use List::MoreUtils::apply.

use List::MoreUtils qw(apply);
print ( (apply { s/$word/reverse $word/ieg } $string), "\n");

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM
use one subroutine's variable value in another subroutine inside a module. king Perl Misc 5 04-29-2007 06:39 AM
How to print modified version of string without intermediate variable? usenet@DavidFilmer.com Perl Misc 12 11-11-2005 09:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57