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

Hi

the following example shows how to "ascii-draw" a tree with one regex!

DB<83> $_="x"x30 DB<84> 1 while s/^( *x+)xx/ $1\n$&/;print "$_\n $1\n $1" xx xxxx xxxxxx xxxxxxxx xxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxxxx xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xx
That's for sure possible in a sole RegEx without while loop just with a /g-modifier!

But how? I experimented with \G but without success..

Cheers Rolf

PS: I know I missed Xmas... ; )

more golfing at Re: Christmas Tree

UPDATE: added 2 missing whitespaces

Replies are listed 'Best First'.
Re: Resetting pos() within a RegEx?
by ikegami (Patriarch) on Jan 10, 2009 at 18:33 UTC
    It's not possible to do in a s/// using the method you requested. If it was, the following would print a series of 'a's
    >perl -le"$|=1; ($_='abc') =~ s/\G(.)/print(pos(),' ',$1); pos()=0/eg" 0 a 1 b 2 c
Re: Resetting pos() within a RegEx?
by Skeeve (Parson) on Jan 10, 2009 at 21:41 UTC

    No loop but 2 regexps:

    $_="x" x 30; s:xx:(" " x (length($')/2))."$`$&\n":ge; s:(?<=\n).*:$&$`$`:s; print;

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Resetting pos() within a RegEx?
by JavaFan (Canon) on Jan 11, 2009 at 01:06 UTC
    That's for sure possible in a sole RegEx without while loop just with a /g-modifier!
    No. A regexp cannot print anything. Somewhere, you need to execute code, either outside a regexy construct, in the replacement part of an s/// with the /e modifier, or by using (?{ }) or (??{ }).

    But if I'm allowed to execute code, than the constraint "do it in a single regexp" isn't a constraint at all. Then I just do /^(?{ code to generate whatever you want })/.

    I suggest to be more specific in what you (dis)allow to have a more fun challenge.

      > I suggest to be more specific in what you (dis)allow to have a more fun challenge.

      It's for golfing, I just want to get shorter code by getting rid of the while-command not neccessarily replacing the print command.

      But if I'm allowed to execute code, than the constraint "do it in a single regexp" isn't a constraint at all.
      So here we go with 3 regexps and no code execution (exept for initializing and printing):

      $_="x" x 30; s:xx:$' $`$&\n:g; s:(?<=\n).*:$&$`$`:s; s/xx(?=(x*)(\1) )/ /g; print;

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        Still many bytes, but I really like your approach of taking the postmatch to indend the substitution! =)

        like this it's quick and easy to get the triangular part of the tree!!!

        DB<<70>> $_=$"x15;s: :$'#$`$`#\n:g;print ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # DB<<71>> $_=$"x15;s: :$_=$`;y/ /#/;"$'$_$_\n":ge;print ## #### ###### ######## ########## ############ ############## ################ ################## #################### ###################### ######################## ########################## ############################
Re: Resetting pos() within a RegEx?
by JavaFan (Canon) on Jan 12, 2009 at 14:02 UTC
    Doing it in a single regexp is possible, although it won't be what you're looking at:
    s{.*} { xx xxxx xxxxxx xxxxxxxx xxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxxxx xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xx}s;