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

U

usenet

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?
 
J

John W. Krahn

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
 
U

usenet

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

Thanks!
 
D

Dr.Ruud

(e-mail address removed):
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";
 
A

Anno Siegel

Dr.Ruud said:
(e-mail address removed):


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
 
D

Dr.Ruud

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.
 
T

Tad McClellan

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: <[email protected]>


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

Dr.Ruud

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: (e-mail address removed)

The Internet standard way to express such a pointer:

<
(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";
 
B

Brian McCauley

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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top