variables in substitution

Discussion in 'Perl Misc' started by Wim, Jan 15, 2007.

  1. Wim

    Wim 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.
     
    Wim, Jan 15, 2007
    #1
    1. Advertising

  2. On 01/15/2007 09:07 AM, Wim wrote:
    > 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




    --
    Windows Vista and your freedom in conflict:
    http://techdirt.com/articles/20061019/102225.shtml
     
    Mumia W. (NOSPAM), Jan 15, 2007
    #2
    1. Advertising

  3. Wim wrote:
    > 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";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 15, 2007
    #3
  4. Wim <> wrote:

    > 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?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 15, 2007
    #4
  5. Brian McCauley, Jan 16, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    Should this substitution be compilable?

    valentin tihomirov, Nov 28, 2004, in forum: VHDL
    Replies:
    12
    Views:
    793
    valentin tihomirov
    Nov 30, 2004
  2. Ian
    Replies:
    4
    Views:
    2,310
    Ben Bacarisse
    Feb 2, 2006
  3. Replies:
    9
    Views:
    961
  4. Dietrich
    Replies:
    3
    Views:
    149
    Joe Smith
    Jul 25, 2004
  5. GarfGarf

    substitution using variables

    GarfGarf, Nov 16, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    122
    GarfGarf
    Nov 17, 2005
Loading...

Share This Page