variables in substitution

W

Wim

Hello,

I am trying to make a substition using rules taken from an array. The
basics of the program are:

my @A= ( 'AName-9.45', 'Another-6.123' ) ;
my @NameMap= (
[ qr/^aname-(\d+)\.(\d+)$/io, 'groupzero-$1-$2' ],
[ qr/^another-(\d+)\.(\d+)$/io, q/groupone-$1-$2/ ] ) ;

foreach my $a ( @A ) {
printf "%-17s -> ", $a ;
$a=~ s/$$_[0]/$$_[1]/i foreach ( @NameMap ) ;
printf "$a\n" ;
}

Matching an input string (taken from @A) succeeds. However, replacing
$1 and $2 by the numbers matched between the parenthesis in de left
part fails. the literal string $1 appears in the result. Using
evaluation doesn't work: it will report an uninitialised value. How
can one take both substitute parameters from an array and use local
variables $1 at the same time?

Regards,
Wim Nelis.
 
G

Guest

Hello,

I am trying to make a substition using rules taken from an array. The
basics of the program are:

my @A= ( 'AName-9.45', 'Another-6.123' ) ;
my @NameMap= (
[ qr/^aname-(\d+)\.(\d+)$/io, 'groupzero-$1-$2' ],
[ qr/^another-(\d+)\.(\d+)$/io, q/groupone-$1-$2/ ] ) ;

foreach my $a ( @A ) {
printf "%-17s -> ", $a ;
$a=~ s/$$_[0]/$$_[1]/i foreach ( @NameMap ) ;
printf "$a\n" ;
}

Matching an input string (taken from @A) succeeds. However, replacing
$1 and $2 by the numbers matched between the parenthesis in de left
part fails. the literal string $1 appears in the result. Using
evaluation doesn't work: it will report an uninitialised value. How
can one take both substitute parameters from an array and use local
variables $1 at the same time?

Regards,
Wim Nelis.

I would do it this way:

my @A= ( 'AName-9.45', 'Another-6.123' ) ;
my @NameMap= (
[ qr/^aname-(\d+)\.(\d+)$/io, 'groupzero-%d-%d' ],
[ qr/^another-(\d+)\.(\d+)$/io, q/groupone-%d-%d/ ] ) ;

foreach my $a ( @A ) {
printf "%-17s -> ", $a ;
$a =~ s/$$_[0]/sprintf($$_[1],$1,$2)/ei foreach @NameMap;
printf "$a\n" ;
}


HTH
 
G

Gunnar Hjalmarsson

Wim said:
I am trying to make a substition using rules taken from an array. The
basics of the program are:

my @A= ( 'AName-9.45', 'Another-6.123' ) ;
my @NameMap= (
[ qr/^aname-(\d+)\.(\d+)$/io, 'groupzero-$1-$2' ],
[ qr/^another-(\d+)\.(\d+)$/io, q/groupone-$1-$2/ ] ) ;

foreach my $a ( @A ) {
printf "%-17s -> ", $a ;
$a=~ s/$$_[0]/$$_[1]/i foreach ( @NameMap ) ;
printf "$a\n" ;
}

Matching an input string (taken from @A) succeeds. However, replacing
$1 and $2 by the numbers matched between the parenthesis in de left
part fails. the literal string $1 appears in the result. Using
evaluation doesn't work: it will report an uninitialised value. How
can one take both substitute parameters from an array and use local
variables $1 at the same time?

This is one way:

my @A = ( 'AName-9.45', 'Another-6.123' ) ;
my @NameMap = (
[ qr/^aname-(\d+)\.(\d+)$/i, '"groupzero-$1-$2"' ],
[ qr/^another-(\d+)\.(\d+)$/i, q/"groupone-$1-$2"/ ]
);

foreach my $a ( @A ) {
printf "%-17s -> ", $a;
$a =~ s/$$_[0]/$$_[1]/eei foreach @NameMap;
print "$a\n";
}
 
T

Tad McClellan

Wim said:
I am trying to make a substition using rules taken from an array. The
basics of the program are:

my @A= ( 'AName-9.45', 'Another-6.123' ) ;
my @NameMap= (
[ qr/^aname-(\d+)\.(\d+)$/io, 'groupzero-$1-$2' ],
[ qr/^another-(\d+)\.(\d+)$/io, q/groupone-$1-$2/ ] ) ;


[ qr/^aname-(\d+)\.(\d+)$/io, '"groupzero-$1-$2"' ],
[ qr/^another-(\d+)\.(\d+)$/io, q/"groupone-$1-$2"/ ] ) ;

foreach my $a ( @A ) {
printf "%-17s -> ", $a ;
$a=~ s/$$_[0]/$$_[1]/i foreach ( @NameMap ) ;


$a=~ s/$$_[0]/$$_[1]/ee foreach ( @NameMap ) ;

printf "$a\n" ;
}

How
can one take both substitute parameters from an array and use local
variables $1 at the same time?


By realizing that the 2nd part of an s/// is a string, and then
checking the Perl FAQ:

perldoc -q string

How can I expand variables in text strings?
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top