Home > Archive > PERL Beginners > June 2005 > How to use qr to eliminate redundant code
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to use qr to eliminate redundant code
|
|
| Siegfried Heintze 2005-06-08, 3:57 am |
| This piece of code works, but it has a redundant pattern in it. Every time I
try to create a variable "my $pRole = qr/Assistant|Executive|Senior//;" to
replace the redundant patterns, it stops working. It has something to do
with the "gi" qualifiers for the patterns. Can someone edit this code to use
the variable $pRole instead of multiple occurrences of
/Assistant|Executive|Senior/?
Thanks,
Siegfried
#!c:\Perl\bin\perl.exe
# Begin commands to execute this file using Perl with bash
# ./test.pl
# End commands to execute this file using Perl with bash
my $sJobTitle = "Wanted: Senior Assistant for Corporate Executive";
while(my @m = $sJobTitle =~ /Assistant|Executive|Senior/gi){
$sJobTitle =~ s/Assistant|Executive|Senior//i;
print __LINE__. " m=[".join(",",@m)."]\n";
$x{$m[0]} = 0;
}
print $sJobTitle."(".join(",",sort keys %x).")\n";
It prints the following:
cd c:/WinOOP/Perl/bots/yahoo/finance/
../test.pl
10 m=[Senior,Assistant,Executive]
10 m=[Assistant,Executive]
10 m=[Executive]
Wanted: for Corporate (Assistant,Executive,Senior)
Compilation finished at Tue Jun 07 10:27:32
| |
| John W. Krahn 2005-06-08, 3:57 am |
| Siegfried Heintze wrote:
> This piece of code works, but it has a redundant pattern in it. Every time I
> try to create a variable "my $pRole = qr/Assistant|Executive|Senior//;"
That won't work because of the syntax error, you have an extra '/' character
at the end. That should be:
my $pRole = qr/Assistant|Executive|Senior/i;
> to replace the redundant patterns, it stops working. It has something to do
> with the "gi" qualifiers for the patterns. Can someone edit this code to use
What makes you think that the "gi" options have anything to do with it?
> the variable $pRole instead of multiple occurrences of
> /Assistant|Executive|Senior/?
>
>
> #!c:\Perl\bin\perl.exe
> # Begin commands to execute this file using Perl with bash
> # ./test.pl
> # End commands to execute this file using Perl with bash
>
> my $sJobTitle = "Wanted: Senior Assistant for Corporate Executive";
> while(my @m = $sJobTitle =~ /Assistant|Executive|Senior/gi){
> $sJobTitle =~ s/Assistant|Executive|Senior//i;
> print __LINE__. " m=[".join(",",@m)."]\n";
> $x{$m[0]} = 0;
> }
> print $sJobTitle."(".join(",",sort keys %x).")\n";
If you want to do that with a single regular expression you _could_ do it like
this:
my $sJobTitle = 'Wanted: Senior Assistant for Corporate Executive';
my %x;
$sJobTitle =~ s/(Assistant|Executive|Senior)/ $x{ $1 } = (); '' /gie;
print "$sJobTitle(" . join( ',', sort keys %x ) . ")\n\n";
Or to use the example in the FAQ which is faster then using alternation in the
regular expression:
my $sJobTitle = 'Wanted: Senior Assistant for Corporate Executive';
my @words = qw( Assistant Executive Senior );
my %x;
for my $word ( @words ) {
$x{ $word } = () if $sJobTitle =~ s/$word//gi;
}
print "$sJobTitle(" . join( ',', sort keys %x ) . ")\n\n";
John
--
use Perl;
program
fulfillment
|
|
|
|
|