http://www.perlmonks.org?node_id=1012471

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

Replies are listed 'Best First'.
Re: Substitution of a particular letter in all combinations of positions in word (see inside)
by LanX (Saint) 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

      ... 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'
      Amazing! Thank you very much
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 :)