regexp substitution

M

Mike Solomon

I have written the following script to replaces items in a line that
are enclosed with braces with variables of the same name

use strict;

my $test = 'mike test {VAR1} hhhh {VAR2}';

my %vars = (VAR1 => 'mike1', VAR2 => 'mike2');

while ($test =~ /{/) {
$test =~ /({.*?})/;
my $new = $1;
$new =~ s/{|}//g;
$test =~ s/{.*?}/$vars{$new}/;
}

print "$test\n";

I am sure there is a better way of writing this and would appreciate
your comments

Thanks

Mike
 
G

Gunnar Hjalmarsson

Mike said:
I have written the following script to replaces items in a line
that are enclosed with braces with variables of the same name

use strict;

my $test = 'mike test {VAR1} hhhh {VAR2}';

my %vars = (VAR1 => 'mike1', VAR2 => 'mike2');

while ($test =~ /{/) {
$test =~ /({.*?})/;
my $new = $1;
$new =~ s/{|}//g;
$test =~ s/{.*?}/$vars{$new}/;
}

print "$test\n";

I am sure there is a better way of writing this and would
appreciate your comments

You can use the /g modifier instead of a while loop.
No need to first capture and then remove the braces.
No need for a temporary variable.

$test =~ s/{(\w+)}/$vars{$1}/g;
 
A

Anno Siegel

Mike Solomon said:
I have written the following script to replaces items in a line that
are enclosed with braces with variables of the same name

use strict;

my $test = 'mike test {VAR1} hhhh {VAR2}';

my %vars = (VAR1 => 'mike1', VAR2 => 'mike2');

Using a hash in this way is standard in this situation. The loop below
is more complicated than it has to be.
while ($test =~ /{/) {

There's no need to search for each "{" singly. Just try to match
the complete pattern. Also, you don't need to escape "{" if it
doesn't look like a "{n,m}" quantifier. You didn't in the following
line:
$test =~ /({.*?})/;

Don't capture things only to get rid of them later. You don't want the
"{}", so don't capture them:

$test =~ /{(.*?)}/;
my $new = $1;

If you want the capture in $new, write that

my ( $new) = $test =~ /{(.*?)}/;
$new =~ s/{|}//g;

Alternations of single characters are better written as character
classes

$new =~ s/[{}]//g;

but deletions of single characters are best done by tr///:

$new =~ tr/{}//d;

However, you don't need that step at all if you don't capture the {}
in the first place.
$test =~ s/{.*?}/$vars{$new}/;
}

print "$test\n";

I am sure there is a better way of writing this and would appreciate
your comments

It can all be done in a single global substitution:

$test =~ s/{(.*?)}/$vars{ $1}/g;

Anno
 
M

Mike Solomon

Mike Solomon said:
I have written the following script to replaces items in a line that
are enclosed with braces with variables of the same name

use strict;

my $test = 'mike test {VAR1} hhhh {VAR2}';

my %vars = (VAR1 => 'mike1', VAR2 => 'mike2');

Using a hash in this way is standard in this situation. The loop below
is more complicated than it has to be.
while ($test =~ /{/) {

There's no need to search for each "{" singly. Just try to match
the complete pattern. Also, you don't need to escape "{" if it
doesn't look like a "{n,m}" quantifier. You didn't in the following
line:
$test =~ /({.*?})/;

Don't capture things only to get rid of them later. You don't want the
"{}", so don't capture them:

$test =~ /{(.*?)}/;
my $new = $1;

If you want the capture in $new, write that

my ( $new) = $test =~ /{(.*?)}/;
$new =~ s/{|}//g;

Alternations of single characters are better written as character
classes

$new =~ s/[{}]//g;

but deletions of single characters are best done by tr///:

$new =~ tr/{}//d;

However, you don't need that step at all if you don't capture the {}
in the first place.
$test =~ s/{.*?}/$vars{$new}/;
}

print "$test\n";

I am sure there is a better way of writing this and would appreciate
your comments

It can all be done in a single global substitution:

$test =~ s/{(.*?)}/$vars{ $1}/g;

Anno

That's perfect thanks everyone
 
J

John W. Krahn

Mike said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in message


That's perfect thanks everyone

And in case the contents in the braces is not present in the hash:

$test =~ s/{([^}]+)}/exists $vars{$1} ? $vars{$1} : $1/eg;


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top