Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Substitution of a particular letter in all combinations of positions in word (see inside)

by Doctrin (Beadle)
on Jan 09, 2013 at 12:55 UTC ( #1012471=perlquestion: print w/ replies, xml ) Need Help??
Doctrin has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone! Could you please tell me how to do the following. Say, we have a word ferber. I wanna substitute "e" with "a" in all combinations, so that I have following words:

farber

ferbar

farbar

Maybe, there is even a "one-line" solution? Thanks in advance

Comment on Substitution of a particular letter in all combinations of positions in word (see inside)
Re: Substitution of a particular letter in all combinations of positions in word (see inside)
by Anonymous Monk on Jan 09, 2013 at 13:08 UTC
    You first :)
Re: Substitution of a particular letter in all combinations of positions in word (see inside)
by LanX (Canon) on Jan 09, 2013 at 13:08 UTC
    DB<182> $s=join '{e,a}',split /e/,"ferber" => "f{e,a}rb{e,a}r" DB<183> @a=< $s > => ("ferber", "ferbar", "farber", "farbar")

    UPDATE

    one-liner

    DB<192> @a=glob(join '{e,a}',split /e/,"ferber") => ("ferber", "ferbar", "farber", "farbar")

    Cheers Rolf

      Amazing! Thank you very much

      ... and using the  /r substitution modifier of 5.14+:

      >perl -wMstrict -le "printf qq{'$_' } for glob('ferber' =~ s{e}{{e,a}}xmsgr); " 'ferber' 'ferbar' 'farber' 'farbar'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2015-07-03 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (51 votes), past polls