Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Substituting in the substitution!

by Mondongo (Beadle)
on May 06, 2004 at 15:32 UTC ( [id://351163]=perlquestion: print w/replies, xml ) Need Help??

Mondongo has asked for the wisdom of the Perl Monks concerning the following question:

I have a string like this
[ABC AB AB12/83]

and I do
s|\[([AZ_]+)\s([AZ_]+)\s([AZ_]+)\]|:$1_$2_$3|g

so I end up with
ABC_AB_AB12/83

I would like to end up with
ABC_AB_AB12_83
changing the "/" to "_"

can it be done?

maybe with
s|\[([AZ_]+)\s([AZ_]+)\s([AZ_]+)\]|:$1_$2_&do_the_change($3)|ge

or something more "inline"... ?

thx to all!

Replies are listed 'Best First'.
Re: Substituting in the substitution!
by Plankton (Vicar) on May 06, 2004 at 15:50 UTC
    $_='ABC AB AB12/83'; tr / \//_/; print

    Plankton: 1% Evil, 99% Hot Gas.

      Why's the space before the backslash?

      Update: ah, I forgot. Perl repeats the last character of the right side of the tr if there are too few. Great.

        Because that way both spaces and slashes get translated into underscores. This strips out the brackets as well:
        $_='[ABC AB AB12/83]'; tr# /[]#__#d; print;

        The PerlMonk tr/// Advocate
Re: Substituting in the substitution!
by duff (Parson) on May 06, 2004 at 15:50 UTC
    Don't you just want something like s![/\s]!_!g; tr/[]//d;. Or am I missing something?
Re: Substituting in the substitution!
by pizza_milkshake (Monk) on May 06, 2004 at 15:47 UTC
    this'll do it
    #!perl -wl use strict; $_ = "[ABC AB AB12/83]"; print "I have a string like this\n$_"; s{\[([A-Z_]+)\s([A-Z_]+)\s([A-Z_]+)(\d+)/(\d+)\]}{$1_$2_$3$4_$5}; print "I would like to end up with\n$_";

    perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"

      I didn't explain myself quite right.

      The "/" can be in any part of the string, so a new regexp won't fix it. What I want is to capture four clusters and then, when substituting, replace the "/" inside the clusters.

      Right now, I did this:
      $do_the_change_mon = sub { $thingy = shift; $thingy =~ tr|/|_|; return $thingy; } $expr = s| \[([A-Z/]+)\s([A-Z/]+)\s([A-Z/]+)\s([A-Z/]+)\s\] |&$do_the_change_mon("$1_$2_$3_$4)|xe;

      Thank you all for your kind answers, monks!

        That's not bad. Consider this. I rolled your A-Z/ loop up and then did the substitution on a copy inside the RHS.

        $expr =~ s(\[((?:[A-Z/]+)\s){4,4})\]){ my $match = $_; $match =~ s([/\s])(_)g; $match; }ge;
Re: Substituting in the substitution!
by perlinux (Deacon) on May 06, 2004 at 17:12 UTC
    IMHO:
    [AZ_] must be [A-Z_]
    the third [AZ_] must be [A-Z0-9_]
    I've found:
    s/^\[([A-Z_]+)\s([A-Z_]+)\s([A-Z0-9]+)\/([0-9]+)\]$/$1_$2_$3_$4/g
    ok? :-) It works but it's not an inline-change on $3!
Re: Substituting in the substitution!
by flyingmoose (Priest) on May 06, 2004 at 19:01 UTC
    I'm a lazy guy here, but isn't the tr after the regex substitution both more easy to read and faster? I think many folks here use regexes a little too much (and I am very amazed with some of your skills), to try to do it all in one pass, when that may not be the most efficient.
Re: Substituting in the substitution!
by Roy Johnson (Monsignor) on May 06, 2004 at 19:37 UTC
    $3 isn't going to match numbers in your example.

    The PerlMonk tr/// Advocate
Re: Substituting in the substitution!
by revdiablo (Prior) on May 06, 2004 at 19:44 UTC

    I'd probably use duff's solution, but here's another way to do it:

    my $str = join "_", split m([ /]), "[ABC AB AB12/83]"; substr($str, 0, 1) = ""; substr($str, -1, 1) = "";

    Update: here's a [very ugly] way to do it all in one line:

    my $str = substr ${\join "_", split m([ /]), "[ABC AB AB12/83]"}, 1, -1;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://351163]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-18 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found