CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
remove substring and assign to new var

 



cauliflower
New User

Sep 2, 2016, 2:43 AM

Post #1 of 7 (3521 views)
remove substring and assign to new var Can't Post

Hi,

I see I can remove a substring like:

$sentence =~ s/substring//;

But I want to assign the resulting string to a new var, I want to leave $sentence unchanged. So I'd have a new var which was $sentence minus the substring. I hope that makes sense?


cauliflower
New User

Sep 2, 2016, 3:11 AM

Post #2 of 7 (3520 views)
Re: [cauliflower] remove substring and assign to new var [In reply to] Can't Post

I think I found an answer using brackets:

($sentence2 = $sentence) =~ s/substring//;


BillKSmith
Veteran

Sep 2, 2016, 7:03 AM

Post #3 of 7 (3515 views)
Re: [cauliflower] remove substring and assign to new var [In reply to] Can't Post

Unless you have a very old version of perl, you can use the /r option.


Code
$sentence2 = $sentence =~ s/substring//r;


It does not make much difference which way you choose unless your original sentence is already in $_.


Code
C:\Users\Bill\perl\guru>type cauliflower.pl 
use strict;
use warnings;
$_ = 'The quick brown fox jumps.';
my $sentence2 = s/quick\s//r;
print $sentence2, "\n";

C:\Users\Bill\perl\guru>cauliflower.pl
The brown fox jumps.

Good Luck,
Bill


cauliflower
New User

Sep 2, 2016, 10:05 AM

Post #4 of 7 (3508 views)
Re: [BillKSmith] remove substring and assign to new var [In reply to] Can't Post

Hmmm that didn't seem to work for me, but we are running v5.8.0, which I guess is pretty old!


FishMonger
Veteran / Moderator

Sep 2, 2016, 10:16 AM

Post #5 of 7 (3506 views)
Re: [cauliflower] remove substring and assign to new var [In reply to] Can't Post


Quote
I want to assign the resulting string to a new var, I want to leave $sentence unchanged.


If you don't want to modify the original var, then don't use a substitution regex.

Code
my $sentence = 'The quick brown fox jumps.';  
my ($sentence2) = $sentence =~ /\s(quick)\s/;
print $sentence2, "\n";



Quote
C:\test>test.pl
quick


I may have misinterpreted your goal, so if that doesn't do what you want, then please clarify your need.


(This post was edited by FishMonger on Sep 2, 2016, 10:18 AM)


cauliflower
New User

Sep 2, 2016, 11:48 AM

Post #6 of 7 (3497 views)
Re: [FishMonger] remove substring and assign to new var [In reply to] Can't Post


In Reply To

Quote
I want to assign the resulting string to a new var, I want to leave $sentence unchanged.


If you don't want to modify the original var, then don't use a substitution regex.

Code
my $sentence = 'The quick brown fox jumps.';  
my ($sentence2) = $sentence =~ /\s(quick)\s/;
print $sentence2, "\n";



Quote
C:\test>test.pl
quick


I may have misinterpreted your goal, so if that doesn't do what you want, then please clarify your need.


Thanks,

In your example what I would actually want sentence2 to be is "The brown fox".

What I ended up needing was to substitute in another string, I'm actually using this format which seems to work:

($sentence2 = $sentence) =~ s/quick/slow/;

Making sentence2 "The slow brown fox"


BillKSmith
Veteran

Sep 2, 2016, 3:06 PM

Post #7 of 7 (3488 views)
Re: [cauliflower] remove substring and assign to new var [In reply to] Can't Post

"Non-Destructive Substitution" (s///r) was added in version 5.14.0.

I consider your approach to the original task as excellent with the older perl and still acceptable with newer versions. I do not understand your reply to FishMonger. Perhaps if you explain your new requirements we can suggest a better solution.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives