in reply to A silly reg exp question

A couple of ways (all untested):
s/([1-8])/"-" x $1/eg; s/1/-/g, s/2/--/g, s/3/---/g, s/4/----/g, s/5/-----/g, s/6/------/g, s +/7/-------/g, s/8/--------/g; my @x = map {"-" x $_} 0..8; s/([1-8])/$x[$1]/g; 1 while s/([2-8])/-@{[$1-1]}/g; s/1/-/g; s/8/7-/g; s/7/33-/g; s/6/5-/g; s/5/22-/g; s/4/3-/g; s/3/11-/g; s/2/1-/ +g; s/1/-/g;

Replies are listed 'Best First'.
Re^2: A silly reg exp question
by AnomalousMonk (Archbishop) on Jan 16, 2012 at 20:42 UTC
    s/8/7-/g; s/7/33-/g; s/6/5-/g; s/5/22-/g; s/4/3-/g; s/3/11-/g; s/2/1-/ +g; s/1/-/g;

    This version certainly seems to work, but is it lazy, crazy, peevish, peccant, or...?

    I think it's the version I would choose if I hated the person who would be maintaining my code!

    Update: ++ for the whole reply for diversity and perversity!

      Feel free to replace the semicolons (except the last) with xors for even more perversity.

        ++ and amen, but only with  no warnings 'void'; since one cannot enjoy perversity incautiously.

        In fact, wouldn't any of the left-associative operators  * x + - . << >> & | ^ , => xor do as well so long as precedence levels were not mixed unwisely? Although I haven't thoroughly tested it, I like:

        >perl -wMstrict -le "$_ = 'p1r4k/8'; my $t = 'p-r----k/--------'; no warnings 'void'; s/8/7-/g-+s/7/33-/g-+s/6/5-/g-+s/5/22-/g-+s/4/3-/g-+s/3/11-/g-+s/2/1- +/g-+s/1/-/g; print qq{'$_'}; print $_ eq $t; " 'p-r----k/--------' 1
Re^2: A silly reg exp question
by chessgui (Scribe) on Jan 16, 2012 at 14:48 UTC
    s/1/-/g, s/2/--/g, s/3/---/g, s/4/----/g, s/5/-----/g, s/6/------/g, s/7/-------/g, s/8/--------/g;

    This is the exact way I have done it :). When you are lazy you end up with code like this. To be sure: this works.