Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^6: substitute characters in the RHS of a search & replace

by AnomalousMonk (Chancellor)
on Mar 09, 2013 at 21:04 UTC ( #1022624=note: print w/replies, xml ) Need Help??

in reply to Re^5: substitute characters in the RHS of a search & replace
in thread substitute characters in the RHS of a search & replace

Hmmm...     This is the best I could do, but it's a bit icky for Anonymonk's OPed application:

>perl -wMstrict -le "sub rx (&@) { return map { local $_ = $_; ()= $_[0]->(); $_; } @_[1..$#_]; } ;; my $s = 'a b c d'; print rx { s/ /_/g } $s; ;; my @ra = ('p q r', 'x y z z y'); print rx { s/ /_/g } @ra; ;; my $x = q{Here are [[a [[ variable]] number [[of ]words]] in brackets}; print qq{'$x'}; ;; $x =~ s( \[\[ (.*?) \]\] )( (rx {s/ /_/g} $1)[0] )xmsge; print qq{'$x'}; " a_b_c_d p_q_rx_y_z_z_y 'Here are [[a [[ variable]] number [[of ]words]] in brackets' 'Here are a_[[_variable number of_]words in brackets'

Bottom line:  s///r is a lot nicer. Thanks again to whoever thought of that one.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022624]
[1nickt]: Go get em talexb
[1nickt]: Corion Yes, once bitten, twice shy with experimental. If you use use experimental qw/ refaliasing declared_refs/; you can do my (\%x, \%y) = @_ when you like hashes but are given hashrefs :-)
[1nickt]: ^ 5.26 required for declared_refs

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-11-17 21:00 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (272 votes). Check out past polls.