[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] RegEx tricks



At 19:32 -0500 6/19/99, Tushar Samant wrote:
> As for tricks, here's one--pretty old and nothing very special--
> but it might be new to someone:
>
>     ($_, $p) = ("*", " " x shift);
>     while (chop $p) {
>         s/(.) /$1$1/g; s/^|$/ /g; s/(.)(.)/$1 eq $2?"  ":"* "/eg;
>         print "$p$_\n";
>     }

It was new to me. And right purty too! (Possible Spoiler, if you haven't
run it yet, you may or may not want to view the output at the far end of
this mail; scroll slowly.)

For those who need more clues, hints, or instruction...

I prefer this broken out a little more; try:

  dosub($ARGV[0]);

  sub dosub() {
      ($_, $p) = ("*", " " x shift);
      while (chop $p) {
          s/(.) /$1$1/g;
          s/^|$/ /g;
          s/(.)(.)/$1 eq $2?"  ":"* "/eg;
          print "$p$_\n";
      }
  }


Then be sure to give it interesting data (try 8, 16, or 32)

It's also fun to put in print statements at each line; it's interesting to
watch the way this thing builds the output...

Who comes up with these things? Anyone got more?

SPOILER - Output of 16 coming below. Don't look if you want to guess what
it does or run it yourself first.














Here's 16




               *
              * *
             *   *
            * * * *
           *       *
          * *     * *
         *   *   *   *
        * * * * * * * *
       *               *
      * *             * *
     *   *           *   *
    * * * *         * * * *
   *       *       *       *
  * *     * *     * *     * *
 *   *   *   *   *   *   *   *
* * * * * * * * * * * * * * * *
-- --
       |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
 ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
      |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
     '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.org

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org