Perl Regex : Modification of a read-only value attempted

I am trying to replace whitespaces in a string after matching it against a regular expression.

my $string = "watch download Buffy the Vampire Slayer Season 1 Episode 1 Gorillavid";

if ($string =~ m!(Season\s\d+\sEpisode\s\d+)!){

    $1 =~ s!\s+!!g;

    say $1;

}

Now when I run the above code I get Modification of a read-only value attempted. Now if I store the value of $1 in a variable than try to perform a substitution on that variable it works fine.

So, is there any way I can perform a substitution in place without creating a new temporary variable.

PS: Can someone show me how to write the above code as one-liner because I am not able to :)

Answers


Don't mess with special variables and just capture the data you want, while construction output yourself.

$string = "watch download Buffy the Vampire Slayer Season 1 Episode 1 Gorillavid";
if ($string =~ m!Season\s(\d+)\sEpisode\s(\d+)!){
   say("Season$1Episode$2");
}

Need Your Help

HTML.TextAreaFor MVC3

asp.net-mvc-3 textarea

I have a large amount of text data that I want to display in something like a HTML.TextAreaFor(). I tried this using the readonly html property. However, the user can still select the date and cl...

JavaScript - remove duplicates algorithm efficiency

javascript algorithm duplicate-removal

I have read a lot about this topic and I saw many different algorithms. I have stumbled across another solution which Im having a hard time to understand its efficiency compared to other algorithms,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.