What is the RHS of a substitution?

Discussion in 'Perl Misc' started by Ben Bullock, May 21, 2006.

  1. Ben Bullock

    Ben Bullock Guest

    This question is about substitutions like

    s/XYZ/ABC/;

    Is there a variable which contains the right hand part (ABC) of the most
    recent substitution? I have a series of complex substitutions as part of a
    while ( .. ) statement, and I would like to be able to use the right hand
    part of the most recent match, but I'm not sure whether there is a simple
    way to do this. Thanks for any help.
    Ben Bullock, May 21, 2006
    #1
    1. Advertising

  2. Ben Bullock wrote:

    > This question is about substitutions like
    >
    > s/XYZ/ABC/;
    >
    > Is there a variable which contains the right hand part (ABC) of the most
    > recent substitution?


    I don't believe so. It seems a rather unusual requirement.

    >I have a series of complex substitutions as part of a
    > while ( .. ) statement, and I would like to be able to use the right hand
    > part of the most recent match, but I'm not sure whether there is a simple
    > way to do this.


    Using the /e qualifier you can put arbitrary Perl code in the RHS, for
    example:

    use strict;
    use warnings;
    $_='abc';

    my $rhs;
    while ( s/([[:lower:]])/$rhs=uc($1)/e ) {
    print "$1 => $rhs\n";
    }
    Brian McCauley, May 21, 2006
    #2
    1. Advertising

  3. Ben Bullock

    Dr.Ruud Guest

    Ben Bullock schreef:

    > This question is about substitutions like
    >
    > s/XYZ/ABC/;
    >
    > Is there a variable which contains the right hand part (ABC) of the
    > most recent substitution? I have a series of complex substitutions as
    > part of a while ( .. ) statement, and I would like to be able to use
    > the right hand part of the most recent match, but I'm not sure
    > whether there is a simple way to do this. Thanks for any help.


    You could put the substitutions in an array, like this:

    #!/usr/bin/perl
    use strict ;
    use warnings ;

    use re 'eval' ; # check perlre!

    use Data::Dumper ;

    my @subst =
    ( ['ABC', '123'],
    ['DEF', 'GHI'],
    ['GHI', '789'],
    ['XYZ', 'ABC'],
    ) ;

    my @done ;

    while (<DATA>)
    {
    chomp ;

    for my $i (0 .. $#subst)
    {
    my ($f, $t) = ( $subst[$i]->[0],
    $subst[$i]->[1],
    ) ;

    s~ $f (?{ push @done,
    [
    "line $.: $_",
    "todo $i: s/$f/$t/",
    ]
    })
    ~$t~xg
    and push @{$done[$#done]}, "result: $_" ;
    }
    }

    print Data::Dumper->Dump( [ \@subst, \@done ],
    [qw(*subst *done)]
    ) ;

    __DATA__
    wqefewq XYZ skjd
    gg g grewg gwg ewrgrewgrewg eagf
    qfh GHI qfe ABC ewq DEF ddsf XYZ wfsqf
    qckjqcd ABC qlkfjwqf

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 21, 2006
    #3
  4. Ben Bullock <> wrote:
    > This question is about substitutions like
    >
    > s/XYZ/ABC/;
    >
    > Is there a variable which contains the right hand part (ABC) of the most
    > recent substitution?



    You can make one:

    s/XYZ/ $last_match = 'ABC' /e;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 21, 2006
    #4
  5. Ben Bullock

    Dr.Ruud Guest

    Dr.Ruud schreef:

    > my ($f, $t) = ( $subst[$i]->[0],
    > $subst[$i]->[1],
    > ) ;


    By using Lexical::Alias, one can do

    alias $subst[$i]->[0], my $f ;
    alias $subst[$i]->[1], my $t ;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 21, 2006
    #5
  6. Ben Bullock

    Dr.Ruud Guest

    Dr.Ruud schreef:

    > my ($f, $t) = ( $subst[$i]->[0],
    > $subst[$i]->[1],
    > ) ;


    Or use Data::Alias and change it to

    my ($f, $t) = alias( $subst[$i]->[0],
    $subst[$i]->[1],
    ) ;

    Data::Alias seems to know a few more tricks than Lexical::Alias.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 21, 2006
    #6
  7. Ben Bullock

    Ben Morrow Guest

    Quoth "Ben Bullock" <>:
    > This question is about substitutions like
    >
    > s/XYZ/ABC/;
    >
    > Is there a variable which contains the right hand part (ABC) of the most
    > recent substitution? I have a series of complex substitutions as part of a
    > while ( .. ) statement, and I would like to be able to use the right hand
    > part of the most recent match, but I'm not sure whether there is a simple
    > way to do this. Thanks for any help.


    Err, just put it in a variable before you start?

    my $foo = 'ABC';
    s/XYZ/$foo/;
    <more stuff in $foo>

    Ben

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
    Ben Morrow, May 21, 2006
    #7
  8. Ben Bullock

    Ben Bullock Guest

    "Ben Morrow" <> wrote in message
    news:...
    >
    > Quoth "Ben Bullock" <>:
    >> This question is about substitutions like
    >>
    >> s/XYZ/ABC/;
    >>
    >> Is there a variable which contains the right hand part (ABC) of the most
    >> recent substitution? I have a series of complex substitutions as part of
    >> a
    >> while ( .. ) statement, and I would like to be able to use the right hand
    >> part of the most recent match, but I'm not sure whether there is a simple
    >> way to do this. Thanks for any help.

    >
    > Err, just put it in a variable before you start?
    >
    > my $foo = 'ABC';
    > s/XYZ/$foo/;
    > <more stuff in $foo>


    Thanks for your help, but this won't work. As I said, I have a series of
    complex substitutions in a while statement. The above solution assumes that
    the right hand side of the substitution is one fixed string in one
    substitution. In fact each RHS involves all kinds of stuff involving $1, $2,
    etc., and there is more than one substitution, because a series of
    substitutions is being used as the truth test in a while statement. I want
    to get the last matched RHS for the most recent round the loop. This is for
    a semi-automated batch editing program.

    However, the use of s/ /$var = /e does make sense, although I haven't tried
    it in practice yet. Thanks to everyone who made a suggestion.
    Ben Bullock, May 22, 2006
    #8
    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. Guest
    Replies:
    5
    Views:
    600
    Kai-Uwe Bux
    Jan 31, 2006
  2. Guest
    Replies:
    1
    Views:
    443
    Luke Meyers
    Jan 31, 2006
  3. bbrady
    Replies:
    2
    Views:
    934
    bbrady
    Jan 27, 2010
  4. fl
    Replies:
    10
    Views:
    416
    Juha Nieminen
    Jan 12, 2012
  5. Mr P
    Replies:
    21
    Views:
    195
    Brian McCauley
    Dec 28, 2005
Loading...

Share This Page