Build a regexp substitution?
Build a regexp substitution?
am 02.11.2007 11:08:19 von oakb
I'm looking for a way to build a substitution (s///) once at the
beginning of my program so that I don't have to evaluate a conditional
and build the substitution each of the thousands of times my program
loops. I know about the $regexp = qr/pattern/ construct, but can't
figure out how to do something equivalent with a substitution.
-Brian
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Build a regexp substitution?
am 02.11.2007 11:36:44 von Brian Raven
Brian H. Oak <> wrote:
> I'm looking for a way to build a substitution (s///) once at the
> beginning of my program so that I don't have to evaluate a
> conditional and build the substitution each of the thousands of times
> my program loops. =
I don't understand what your question. Perhaps you could write a small
script that demonstrates what you are trying to do.
> I know about the $regexp =3D qr/pattern/ construct,
> but can't figure out how to do something equivalent with a
> substitution. =
my $regexp =3D qr/pattern/;
s/$regexp/something else/;
Is that what you mean?
HTH
-- =
Brian Raven =
==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================
The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =
Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.
L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.
Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Build a regexp substitution?
am 02.11.2007 15:45:25 von Mathieu Longtin
--===============2057785168==
Content-Type: multipart/alternative;
boundary="----=_Part_16002_24379154.1194014725386"
------=_Part_16002_24379154.1194014725386
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
As somebody else pointed out: s/$regexp/whatever/
If you need the whatever to be evaluated early as well, you could build a
sub
my $regexp = qr/.../;
my $replace = ...;
my $replacer = sub { s/$regexp/$replace/; };
Then in your loop:
$_ = whateverstringneedsreplacing;
$replacer->();
The result of the substitution is in $_.
You could also do something cleaner with parameters:
my $replacer = sub { $_[0] =~ s/$regexp/$replace/; return $_[0] };
Then in the loop:
my $subresult = $replacer->($whateverstringneedsreplacing);
Note that you could use
$replacer = eval "sub {...}" ;
instead of
$replacer = sub { ... } ;
so that perl compiles the regex only once. Not entirely if it would speed it
up.
-Mathieu
I'm looking for a way to build a substitution (s///) once at the
> beginning of my program so that I don't have to evaluate a conditional
> and build the substitution each of the thousands of times my program
> loops. I know about the $regexp = qr/pattern/ construct, but can't
> figure out how to do something equivalent with a substitution.
>
> -Brian
>
>
------=_Part_16002_24379154.1194014725386
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
As somebody else pointed out: s/$regexp/whatever/
If you need the whatever to be evaluated early as well, you could build a sub
my $regexp = qr/.../;
my $replace = ...;
my $replacer = sub { s/$regexp/$replace/; };
Then in your loop:
$_ = whateverstringneedsreplacing;
$replacer->();
The result of the substitution is in $_.
You could also do something cleaner with parameters:
my $replacer = sub { $_[0] =~ s/$regexp/$replace/; return $_[0] };
Then in the loop:
my $subresult = $replacer->($whateverstringneedsreplacing);
Note that you could use
$replacer = eval "sub {...}" ;
instead of
$replacer = sub { ... } ;
so that perl compiles the regex only once. Not entirely if it would speed it up.
-Mathieu
I'm looking for a way to build a substitution (s///) once at the
beginning of my program so that I don't have to evaluate a conditional
and build the substitution each of the thousands of times my program
loops. I know about the $regexp = qr/pattern/ construct, but can't
figure out how to do something equivalent with a substitution.
-Brian
------=_Part_16002_24379154.1194014725386--
--===============2057785168==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============2057785168==--
RE: Build a regexp substitution?
am 02.11.2007 16:03:10 von oakb
Yes, that is sort of what I meant... but not totally. Funny thing is,
I found out about the ability to use the $regexp variable on the left
side of the s/// shortly after originally posting my question. In all
of the research that I have done on this obstacle, I have found that
ability only mentioned in one place, perldoc perlop.
Here is the sample script you asked for:
############################################################
# Position and character would normally be determined
# by user input, but are hard-coded here for brevity
my $position = 2; # Is always a number, 1-4
my $character = "~"; # Can be any "special" char
# Build substitution for later use
my ( $pattern, $substitute );
if ( $position == 1 ) { # s/^(.*)$/~$1/
$pattern = qr/^(.*)$/;
$substitute = "$character\$1";
}
elsif ( $position == 2 ) { # s/^(.*)(\.[^.]+)$/$1~$2/
$pattern = qr/^(.*)(\.[^.]+)$/;
$substitute = "\$1$character\$2";
}
elsif ( $position == 3 ) { # s/^(.*\.)([^.]+)$/$1~$2/
$pattern = qr/^(.*\.)([^.]+)$/;
$substitute = "\$1$character\$2";
}
elsif ( $position == 4 ) { # s/^(.*)$/$1~/
$pattern = qr/^(.*)$/;
$substitute = "\$1$character";
}
else {
die "Invalid position: $position\n";
}
# Loop thousands of times
for ( 0..99_000 ) {
# Filename would be different every time
my $filename = "filename.plus.ext";
$filename =~ s/$pattern/$substitute/;
print "$filename\n";
}
############################################################
I've also tried it this way, with anonymous subroutines:
############################################################
<...snip...>
elsif ( $position == 2 ) {
$pattern = qr/^(.*)(\.[^.]+)$/;
$substitute = sub { return "\$1$character\$2" };
}
<...snip...>
for ( 0..99_000 ) {
# Filename would be different every time
my $filename = "filename.plus.ext";
$filename =~ s/$pattern/$substitute->()/e;
print "$filename\n";
}
############################################################
Unfortunately, both of these methods simply turn $filename into a
literal "$1~$2". In your "s/$regexp/something else/;" it's the
"something else" that's killing me.
Hopefully you can see what I am trying to do here, and why. I'm sure
that there is a way to do this, I just haven't been able to figure it
out yet. There's no idiot like a blind idiot. :P
Thank you,
-Brian
-----Original Message-----
Brian H. Oak <> wrote:
> I'm looking for a way to build a substitution (s///) once at the
> beginning of my program so that I don't have to evaluate a
> conditional and build the substitution each of the thousands of
times
> my program loops.
I don't understand what your question. Perhaps you could write a small
script that demonstrates what you are trying to do.
> I know about the $regexp = qr/pattern/ construct,
> but can't figure out how to do something equivalent with a
> substitution.
my $regexp = qr/pattern/;
s/$regexp/something else/;
Is that what you mean?
HTH
--
Brian Raven
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Build a regexp substitution? SOLVED!
am 02.11.2007 16:39:48 von oakb
Mathieu,
Thank you, you gave me just the clue I needed! BTW, the parameter
thing is required or else it returns a blank string. Here's what
works:
############################################################
# Position and character would normally be determined
# by user input, but are hard-coded here for brevity
my $position = 2; # Is always a number, 1-4
my $character = "~"; # Can be any "special" char
# Build substitution for later use
my $substitute;
if ( $position == 1 ) { # s/^(.*)$/~$1/
$substitute = sub { $_[0] =~ s/^(.*)$/$character$1/; return $_[0] };
}
elsif ( $position == 2 ) { # s/^(.*)(\.[^.]+)$/$1~$2/
$substitute = sub { $_[0] =~ s/^(.*)(\.[^.]+)$/$1$character$2/; return
$_[0] };
}
elsif ( $position == 3 ) { # s/^(.*\.)([^.]+)$/$1~$2/
$substitute = sub { $_[0] =~ s/^(.*\.)([^.]+)$/$1$character$2/; return
$_[0] };
}
elsif ( $position == 4 ) { # s/^(.*)$/$1~/
$substitute = sub { $_[0] =~ s/^(.*)$/$1$character/; return $_[0] };
}
else {
die "Invalid position: $position\n";
}
# Loop thousands of times
for ( 0..99_000 ) {
# Filename would be different every time
my $filename = "filename.plus.ext";
#$filename = $substitute->( $filename );
$substitute->( $filename );
print "$filename\n";
}
############################################################
I really appreciate the suggestions I received from you and Brian
Raven.
-Brian
-----Original Message-----
Mathieu Longtin wrote:
As somebody else pointed out: s/$regexp/whatever/
If you need the whatever to be evaluated early as well, you could
build a sub
my $regexp = qr/.../;
my $replace = ...;
my $replacer = sub { s/$regexp/$replace/; };
Then in your loop:
$_ = whateverstringneedsreplacing;
$replacer->();
The result of the substitution is in $_.
You could also do something cleaner with parameters:
my $replacer = sub { $_[0] =~ s/$regexp/$replace/; return $_[0] };
Then in the loop:
my $subresult = $replacer->($whateverstringneedsreplacing);
Note that you could use
$replacer = eval "sub {...}" ;
instead of
$replacer = sub { ... } ;
so that perl compiles the regex only once. Not entirely if it would
speed it up.
-Mathieu
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Build a regexp substitution? SOLVED!
am 02.11.2007 17:32:32 von Brian Raven
Brian H. Oak <> wrote:
> Mathieu,
> =
> Thank you, you gave me just the clue I needed! BTW, the parameter
> thing is required or else it returns a blank string. Here's what
> works: =
> =
> ############################################################
> # Position and character would normally be determined # by user
> input, but are hard-coded here for brevity =
> my $position =3D 2; # Is always a number, 1-4
> my $character =3D "~"; # Can be any "special" char
> =
> # Build substitution for later use
> my $substitute;
> if ( $position == 1 ) { # s/^(.*)$/~$1/
> $substitute =3D sub { $_[0] =3D~ s/^(.*)$/$character$1/; return $_[0] }; }
> elsif ( $position == 2 ) { # s/^(.*)(\.[^.]+)$/$1~$2/
> $substitute =3D sub { $_[0] =3D~ s/^(.*)(\.[^.]+)$/$1$character$2/;
> return $_[0] }; } =
> elsif ( $position == 3 ) { # s/^(.*\.)([^.]+)$/$1~$2/
> $substitute =3D sub { $_[0] =3D~ s/^(.*\.)([^.]+)$/$1$character$2/;
> return $_[0] }; } =
> elsif ( $position == 4 ) { # s/^(.*)$/$1~/
> $substitute =3D sub { $_[0] =3D~ s/^(.*)$/$1$character/; return $_[0] };
> } else { die "Invalid position: $position\n"; }
> =
> # Loop thousands of times
> for ( 0..99_000 ) {
> # Filename would be different every time
> my $filename =3D "filename.plus.ext";
> #$filename =3D $substitute->( $filename );
> $substitute->( $filename );
> print "$filename\n";
> }
> ############################################################
Actually you don't need the return. In a subroutine the elements of @_
are aliases to the original parameters, so modifying $_[0] modifies (or
tries to) the original parameter. See 'perldoc perlsub'. If you want to
use a return value (which would be my personal preference for this sort
of thing), you should localise the parameter before changing it in the
sub (i.e. "my"), and assign the return value where you call the sub.
Also, that "if" statement screams "array" to me. Plus, you don't need a
regexp matching operation to simply add a single character to the start
or end of a string. So, I think your code could be a bit simpler...
use strict;
use warnings;
my $character =3D '~';
my $position =3D 4;
my @transforms
=3D (sub { $_[0] =3D "${character}$_[0]" },
sub { $_[0] =3D~ s/(\.[^.]+)$/${character}$1/; },
sub { $_[0] =3D~ s/([^.]+)$/${character}$1/; },
sub { $_[0] =3D "$_[0]${character}" }
);
my $sub =3D $transforms[$position - 1];
defined $sub or die "Invalid position: $position\n";
for (0..10) {
my $filename =3D "filename.plus.ext";
$sub->($filename);
print "$filename\n";
}
(Note the use of "use strict; use warnings;". I strongly recommend them
both.)
HTH
-- =
Brian Raven =
==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================
The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =
Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.
L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.
Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs