SparceMatrix
Technical User
I am trying to apply arrays in creating sequences of global Search and Replace Regular Expressions. I have the PERL Bookshelf CD 4.0 and have looked at the discussion in Chapter 7 on using the "qr// operator. But I have yet to apply a successful strategy using the substitution method I demonstrate below.
Here is the simple result that I am trying to achieve. The example is strictly an exercise and is not expected to make sense except as an abstraction of the problem.
Now here I apply arrays to sort out the various steps in the Regular Expressions. I want it to work exactly the way the above code does, but this is as close as I've gotten.
In assigning the strings to the array to be used later as regular expressions, I made progress when I started applying the "qr//" operator. But it remains that I cannot manage to apply the $1 variable. It seems like there should be a way to do this.
Any and all tips or clues would be appreciated. Some references would help.
Here is the simple result that I am trying to achieve. The example is strictly an exercise and is not expected to make sense except as an abstraction of the problem.
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $MyString = "1 The\n2 quick\n3 brown\n4 fox\n5 jumps\n6 over\n7 the\n8 lazy\n9 dog.";
print $MyString;
print "\n\n";
# Uncomment the print statements below to see the different steps.
$MyString =~ s/(t.+)/$1-$1/gi;
# print $MyString;
# print "\n\n";
$MyString =~ s/\n?\d\s/ /g;
# print $MyString;
# print "\n\n";
$MyString =~ s/(quick)/$1est/g;
print $MyString;
print "\n\n";
Now here I apply arrays to sort out the various steps in the Regular Expressions. I want it to work exactly the way the above code does, but this is as close as I've gotten.
Code:
#!/usr/bin/perl
#use strict;
#use warnings;
my $MyString = "1 The\n2 quick\n3 brown\n4 fox\n5 jumps\n6 over\n7 the\n8 lazy\n9 dog.";
$MySub[0][0] = qr/(t.+)/;
$MySub[1][0] = qr/\n?\d\s/;
$MySub[2][0] = qr/(quick)/;
$MySub[0][1] = "$1-$1";
$MySub[1][1] = " ";
$MySub[2][1] = "$1est";
foreach my $Search (@MySub) {
$MyString =~ s/@$Search[0]/@$Search[1]/g;
# Uncomment the print statements below to see the different steps.
# print $MyString;
# print "\n\n";
}
print $MyString;
print "\n\n";
In assigning the strings to the array to be used later as regular expressions, I made progress when I started applying the "qr//" operator. But it remains that I cannot manage to apply the $1 variable. It seems like there should be a way to do this.
Any and all tips or clues would be appreciated. Some references would help.