Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > can s/// return a new value, rather than modifying it's input argument?

Reply
Thread Tools

can s/// return a new value, rather than modifying it's input argument?

 
 
Bill Keese
Guest
Posts: n/a
 
      02-18-2004
You can think of the s/// operator as a function taking three
arguments: pattern, replacement, and input-string. This function
modifies input-string according to pattern and replacement. But is
there any similar function which returns a new string, rather than
updating input-string?

For example, instead of doing this:

($newLetter = $oldLetter) =~ s/Mister/Mr./;

Can I do something like this?

$newLetter = ($oldLetter ~ s/Mister/Mr./) ;

This is similar to java's replace function:
newLetter = oldLetter.replace("Mister", "Mr.");

Thanks,
Bill
 
Reply With Quote
 
 
 
 
Bill
Guest
Posts: n/a
 
      02-18-2004
Bill Keese wrote:
> You can think of the s/// operator as a function taking three
> arguments: pattern, replacement, and input-string. This function
> modifies input-string according to pattern and replacement. But is
> there any similar function which returns a new string, rather than
> updating input-string?
>
> For example, instead of doing this:
>
> ($newLetter = $oldLetter) =~ s/Mister/Mr./;
>
> Can I do something like this?
>
> $newLetter = ($oldLetter ~ s/Mister/Mr./) ;


Change your parentheses.

my $oldLetter = 'Mister Smith';
(my $newLetter = $oldLetter) =~ s/Mister/Mr./;
print "old $oldLetter, new $newLetter\n";



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-18-2004
Bill Keese wrote:
> You can think of the s/// operator as a function taking three
> arguments: pattern, replacement, and input-string. This function
> modifies input-string according to pattern and replacement. But is
> there any similar function which returns a new string, rather than
> updating input-string?
>
> For example, instead of doing this:
>
> ($newLetter = $oldLetter) =~ s/Mister/Mr./;


Not sure what's the problem with doing so. But you can of course write
your own function:

sub replace {
my ($str, $pat, $rpl) = @_;
$str =~ s/$pat/$rpl/;
$str
}

my $newLetter = replace($oldLetter, qr/Mister/, 'Mr.');

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
James Taylor
Guest
Posts: n/a
 
      02-18-2004
In article <c0v2bs$1bmfg6$(E-Mail Removed)-berlin.de>,
Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>
> ...you can of course write your own function:
>
> sub replace {
> my ($str, $pat, $rpl) = @_;
> $str =~ s/$pat/$rpl/;
> $str
> }
>
> my $newLetter = replace($oldLetter, qr/Mister/, 'Mr.');


What's the benefit of using the qr// quoting construct here?

Is there any way for the subroutine to detect that it has been
passed a regex instead of a plain string?

--
James Taylor, Cheltenham, Gloucestershire, UK. PGP key: 3FBE1BF9
To protect against spam, the address in the "From:" header is not valid.
In any case, you should reply to the group so that everyone can benefit.
If you must send me a private email, use james at oakseed demon co uk.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-18-2004
James Taylor wrote:
> In article <c0v2bs$1bmfg6$(E-Mail Removed)-berlin.de>,
> Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>
>>...you can of course write your own function:
>>
>> sub replace {
>> my ($str, $pat, $rpl) = @_;
>> $str =~ s/$pat/$rpl/;
>> $str
>> }
>>
>> my $newLetter = replace($oldLetter, qr/Mister/, 'Mr.');

>
> What's the benefit of using the qr// quoting construct here?


None that I'm aware of in this simple example, but it might be useful
to pass modifiers etc.

> Is there any way for the subroutine to detect that it has been
> passed a regex instead of a plain string?


Don't think so. It is a plain string, btw. In this case it is:
'(?-xism:Mister)'.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      02-18-2004
Also sprach James Taylor:
> In article <c0v2bs$1bmfg6$(E-Mail Removed)-berlin.de>,
> Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>>
>> ...you can of course write your own function:
>>
>> sub replace {
>> my ($str, $pat, $rpl) = @_;
>> $str =~ s/$pat/$rpl/;
>> $str
>> }
>>
>> my $newLetter = replace($oldLetter, qr/Mister/, 'Mr.');

>
> What's the benefit of using the qr// quoting construct here?
>
> Is there any way for the subroutine to detect that it has been
> passed a regex instead of a plain string?


Yes, sure:

sub replace {
my ($str, $pat, $rpl) = @_;
print "Regexp passed\n" if ref($pat) eq "Regexp";
...
}

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Gregory Toomey
Guest
Posts: n/a
 
      02-18-2004
Bill Keese wrote:

> You can think of the s/// operator as a function taking three
> arguments: pattern, replacement, and input-string. This function
> modifies input-string according to pattern and replacement. But is
> there any similar function which returns a new string, rather than
> updating input-string?
>
> For example, instead of doing this:
>
> ($newLetter = $oldLetter) =~ s/Mister/Mr./;
>
> Can I do something like this?
>
> $newLetter = ($oldLetter ~ s/Mister/Mr./) ;
>
> This is similar to java's replace function:
> newLetter = oldLetter.replace("Mister", "Mr.");
>
> Thanks,
> Bill


I think you are asking: can I treat s/// like a function??
The answer is YES, and you can use this trick with other operators too.


sub apply (&$) {
local $_ = $_[1];
$_[0]->();
$_;
}

$var2= apply {s/123/456/g; tr/a-a/A-Z/} $var;


gtoomey
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-18-2004
Bill <(E-Mail Removed)> wrote:
> Bill Keese wrote:


>> ($newLetter = $oldLetter) =~ s/Mister/Mr./;


>> $newLetter = ($oldLetter ~ s/Mister/Mr./) ;



> Change your parentheses.



I think he already knows that...


> (my $newLetter = $oldLetter) =~ s/Mister/Mr./;



--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-18-2004
Tassilo v. Parseval wrote:
> Also sprach James Taylor:
>> Gunnar Hjalmarsson wrote:
>>>
>>> ...you can of course write your own function:
>>>
>>> sub replace {
>>> my ($str, $pat, $rpl) = @_;
>>> $str =~ s/$pat/$rpl/;
>>> $str
>>> }
>>>
>>> my $newLetter = replace($oldLetter, qr/Mister/, 'Mr.');

>>
>> What's the benefit of using the qr// quoting construct here?
>>
>> Is there any way for the subroutine to detect that it has been
>> passed a regex instead of a plain string?

>
> Yes, sure:
>
> sub replace {
> my ($str, $pat, $rpl) = @_;
> print "Regexp passed\n" if ref($pat) eq "Regexp";
> ...
> }


Interesting! I was obviously wrong when saying that qr// returns a
string, even if it looks that way when you print the return value.

Still confused:
- Does qr// return a reference to a regular expression?
- Is this use of the ref() function documented anywhere?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      02-18-2004
On 17 Feb 2004 22:29:30 -0800, (E-Mail Removed) (Bill Keese) wrote:

>modifies input-string according to pattern and replacement. But is
>there any similar function which returns a new string, rather than
>updating input-string?
>
>For example, instead of doing this:
>
>($newLetter = $oldLetter) =~ s/Mister/Mr./;
>
>Can I do something like this?
>
> $newLetter = ($oldLetter ~ s/Mister/Mr./) ;


You can do some tricks to that effect. Other than waht you've already
been suggested, just two more WTDI:

#!/usr/bin/perl

use strict;
use warnings;

my $old="foo bar baz\n";
my $new=do { local $_=$old;
s/b\w+\b/$&$&/g;
$_ };

print $old, $new;

__END__

or

#!/usr/bin/perl

use strict;
use warnings;

my $old="foo bar baz\n";
my $new=sub { local $_=shift;
s/b\w+\b/$&$&/g;
$_ }->($old);

print $old, $new;

__END__

and as usual you have a virtually unlimited number of possible
variations...


Michele
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
 
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: Why is the return type of count_if() "signed" rather than"unsigned"? xmllmx C++ 6 06-23-2010 01:18 PM
Re: Why is the return type of count_if() "signed" rather than "unsigned"? Bo Persson C++ 3 06-22-2010 06:40 PM
Why is the return type of count_if() "signed" rather than "unsigned"? xmllmx C++ 2 06-22-2010 10:21 AM
How to return a stream of XML rather than a dataset JJA ASP .Net 0 12-01-2006 05:08 PM
most software jobs are software maintenance rather than new development? apngss@yahoo.com Java 124 11-12-2005 07:23 PM



Advertisments