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

Hi, I was just wondering if anyone has ever used the reg-ex engine of perl to perform (repeated) substitutions that happen to mimic Conways life? (or something similar) I ask because I have extended the SDL (www.libsdl.org) bindings to perl so that I can have a perl string directly map to a renderable screen bitmap. Perhaps a new competition if 'life type stuff' is doable: regex Frames Per Second / reg-ex art awards !? A nice thing might be to use shared memory and a couple of processes/threads, so one could actually watch the reg-ex engine 'do it's thing' on screen. Thoughts? Regards Wayne

Replies are listed 'Best First'.
Re: life in a reg-ex
by japhy (Canon) on Jul 18, 2000 at 21:40 UTC
    You can do 2d regexes like so:
    #!/usr/bin/perl $string = << "END"; ABC DEF GHI END for $a (0 .. 2){ for $b (0 .. 2) { ($u,$l,$P,$r,$d) = prox($string, $a, $b); print "$P: u=$u, l=$l, r=$r, d=$d\n"; } } sub prox { my ($str,$r,$c) = @_; my $rlen = index($str, "\n"); my $skip = '.' x (($rlen + 1) * $r + $c); my $b = '.' x $rlen; my $f = '.' x $rlen; return $str =~ m{ ^ (?s:$skip) # get to specific row and column (?s:(?<=(.)$b)?) # previous row ($1) (?<=(.))? # previous column ($2) (.) # this row, this column ($3) (?=(.))? # next column ($4) (?s:(?=$f(.))?) # next row ($5) }x; }
    You can also see this (if it got displayed crappily) at http://www.pobox.com/~japhy/regexes/proximity
Re: life in a reg-ex
by barse (Scribe) on Jul 18, 2000 at 12:14 UTC
    Interesting idea.

    It would be nice to have some way to match patterns in two (or more) dimensions. Some kind of matrix reg-exps maybe? Does anybody know if such a thing exists? Is it possible to do using standard reg-exes?
      I was thinking that it might be possible to use the reg-ex range operators { and } to somehow give the reg-ex some concept of rows. thus allowing some kind of 2d
RE: life in a reg-ex
by barndoor (Pilgrim) on Jul 18, 2000 at 12:46 UTC
    I haven't thought about this too much but if you could serialize the matrix into a stream which could then be regex'd, then you would be halfway there. Life requires a bit of counting of cells which I'm not sure how you would achieve but otherwise it sounds interesting. I've read some books on Artifical Life and there are many examples where a 1D (single row) version of life with different rules has been run. You print a row for each iteration and the resulting printout can be very complex. This would seem even more suited to regex processing.
Re: life in a reg-ex
by Anonymous Monk on Jul 18, 2000 at 14:35 UTC
    Sorry I can't offer any advice on the question, but was just thinking that SDL bindings to Perl sound very interesting.

    Will you make these bindings available to others to play with?

    dmtelf

      but of course! perl is an adventure playground after all. someone called David J. Goehrig actually started the bindings, you can find the SDL related post here: http://www.lokigames.com/ml/sdl/5903.html however, since that release, I have updated the bindings: added more SDL functionality. fixed some bugs, added OpenGL support. written some perl demo/scroller/intro type things translated the SDL OpenGL tutorials to perl (for what they are and screenshots of C ones please see: http://www.libsdl.org/opengl/intro.html) if you want my updated SDL-perl please email me for the URL, because I have not been able to contact the original author , please consider mine unofficial. my email: tripix@metaverse.fsnet.co.uk Regards Wayne