Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

My Conway's Game Of Life Attempt

by Anonymous Monk
on Jan 02, 2010 at 21:44 UTC ( [id://815342]=perlquestion: print w/replies, xml ) Need Help??

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

You've heard it before and you you've seen it before. I still tried to quickly implement my own solution of Conway's game in perl, for my own amusement. Albeit being a simple game, I still failed and can't figure out why. I've tested my code multiple times and although it does produce some correct results (e.g. static patterns), most oscillators (exp. Beacon) fail. For reference how it should work: http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Example Patters

#First attempt Game of Life 39x39 @wholepattern = ( [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ','0','0','0',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '] ); #new wholepattern2 @wholepattern2 = @wholepattern; print "iterations: "; $z = <stdin>; $z--; $t = 0; print "t:$t"; print "\n"; $t++; $i=0; foreach $row (@wholepattern) { $j=0; foreach $one (@$row) { if ($wholepattern[$i][$j] eq '0') { print "0|"; } else { print " |"; } $j++; if ($j == 39) { print "\n"; } } $i++; } print "\n"; select(undef, undef, undef, 1); while ($t <= $z) { $i=0; foreach $row (@wholepattern) { $j=0; foreach $one (@$row) { if ($wholepattern[$i-1][$j-1] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$i-1][$j] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$i-1][$j+1] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$i][$j-1] eq '0') { push (@neighbours, '0'); } #if ($wholepattern[$i][$j] eq '0') # { # push (@neighbours, '0'); # } if ($wholepattern[$i][$j+1] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$i+1][$j-1] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$i+1][$j] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$i+1][$j+1] eq '0') { push (@neighbours, '0'); } $neighbours = @neighbours; if ($neighbours < 2) { $wholepattern2[$i][$j] = ' '; } if ($neighbours == 2) { } if ($neighbours == 3) { $wholepattern2[$i][$j] = '0'; } if ($neighbours > 3) { $wholepattern2[$i][$j] = ' '; } @neighbours = (); $j++; } $i++; } #displaying print "t:$t"; print "\n"; $k=0; foreach $row (@wholepattern2) { $l=0; foreach $one (@$row) { if ($wholepattern2[$k][$l] eq '0') { print "0|"; } else { print " |"; } $l++; if ($l == 39) { print "\n"; } } $k++; } print "\n"; $t++; select(undef, undef, undef, 0.4); @wholepattern = @wholepattern2; }

sorry for the long code!

Replies are listed 'Best First'.
Re: My Conway's Game Of Life Attempt
by Khen1950fx (Canon) on Jan 03, 2010 at 02:15 UTC
    Don't forget to add use strict; and use warnings; at the beginning of your script. Have you tried Game::Life? Here's an example from the docs:
    #!/usr/bin/perl use strict; use warnings; use Game::Life; my $game = new Game::Life( 20 ); my $starting = [ [ 1, 1, 1 ], [ 1, 0, 0 ], [ 0, 1, 0 ] ]; $game->place_points( 10, 10, $starting ); for (1..20) { my $grid = $game->get_grid(); foreach ( @$grid ) { print map { $_ ? 'X' : '.' } @$_; print "\n"; } print "\n\n"; $game->process(); }
      Well I've used warnings and strict as well and I don't think the listed errors are part of the problem. Nonetheless I tried the modifiy the code but it still gives me the error message: "Use of uninitialized value in string eq at gol/3.pl line 171, <stdin> line 1." This only affects some of the "if-neighbour" statements: notably lines 171, 183, 187, 191, 195; Marked with an # at the end. It cant really make any sense out of this...
      #First attempt Game of Life 39x39 use strict; use warnings; my @wholepattern = ( [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ','0','0','0',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ','0'], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ','0'], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ','0',' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ',' ',' + ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',' ',' ',' ','0',' + ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ','0',' ',' ',' ',' ',' ',' +0',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ','0',' ',' ',' ','0',' ',' +0','0',' ',' ',' ',' ','0',' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',' ',' ',' ',' ',' ',' +0',' ',' ',' ',' ',' ',' ',' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',' ',' ',' ','0',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ',' ','0'], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' +,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', +' ',' ',' ',' ',' ',' ',' ','0','0'] ); #new wholepattern2 my @wholepattern2 = @wholepattern; print "iterations: "; my $z = <stdin>; $z--; my $t = 0; my $row = (); my $one = (); my @neighbours = (); print "t:$t"; print "\n"; $t++; my $i=0; foreach $row (@wholepattern) { my $j=0; foreach $one (@$row) { if ($wholepattern[$i][$j] eq '0') { print "0"; } else { print " "; } $j++; if ($j == 78) { print "\n"; } } $i++; } print "\n"; select(undef, undef, undef, 1); while ($t <= $z) { my $i=0; foreach $row (@wholepattern) { my $j=0; foreach $one (@$row) { my $a1 = $i - 1; my $a2 = $i; my $a3 = $i + 1; my $b1 = $j - 1; my $b2 = $j; my $b3 = $j + 1; if ($a1 == -1) { my $a1 = 78; } if ($a3 == 79) { my $a3 = 0; } if ($b1 == -1) { my $b1 = 78; } if ($b3 == 79) { my $b3 = 0; } my $e11 = $wholepattern[$a1][$b1]; my $e12 = $wholepattern[$a1][$b2]; my $e13 = $wholepattern[$a1][$b3]; my $e21 = $wholepattern[$a2][$b1]; my $e22 = $wholepattern[$a2][$b2]; my $e23 = $wholepattern[$a2][$b3]; my $e31 = $wholepattern[$a3][$b1]; my $e32 = $wholepattern[$a3][$b2]; my $e33 = $wholepattern[$a3][$b3]; my $cell = "0"; if ($e11 eq "0") { push (@neighbours, '0'); } if ($e12 eq "0") { push (@neighbours, '0'); } if ($e13 eq "0") # { push (@neighbours, '0'); } if ($e21 eq '0') { push (@neighbours, '0'); } #if ($e22 eq '0') # { # push (@neighbours, '0'); # } if ($e23 eq '0') # { push (@neighbours, '0'); } if ($e31 eq '0') # { push (@neighbours, '0'); } if ($e32 eq '0') # { push (@neighbours, '0'); } if ($e33 eq '0') # { push (@neighbours, '0'); } my $neighbours = @neighbours; if ($neighbours < 2) { $wholepattern2[$a2][$b2] = ' '; } if ($neighbours == 2) { } if ($neighbours == 3) { $wholepattern2[$a2][$b2] = '0'; } if ($neighbours > 3) { $wholepattern2[$a2][$b2] = ' '; } @neighbours = (); $neighbours = (); $j++; } $i++; } #displaying print "t:$t"; print "\n"; my $k=0; foreach $row (@wholepattern2) { my $l=0; foreach $one (@$row) { if ($wholepattern2[$k][$l] eq '0') { print "0"; } else { print " "; } $l++; if ($l == 78) { print "\n"; } } $k++; } print "\n"; $t++; select(undef, undef, undef, 0.3); @wholepattern = @wholepattern2; }

        It is so hard to see the flow of your code given its horribly spaced verbosity. Try this version which simply uses perl's inate abilty to be concise and see if it doesn't ring some bells:

        #! perl -slw use strict; my @wholepattern = ( [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ','0','0','0',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' + ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' +',' ',' ',' ',' '] ); my @wholepattern2 = @wholepattern; printf "iterations: "; my $z = <stdin>; $z--; my $t = 0; print "t:$t"; $t++; my $i=0; for my $row (@wholepattern) { my $j=0; for my $one (@$row) { if( $wholepattern[$i][$j] eq '0' ) { printf "0|"; } else { printf " |"; } if( ++$j == 39) { print ""; } } $i++; } select(undef, undef, undef, 1); while( $t <= $z ) { my @neighbours; my $i = 0; for my $row ( @wholepattern ) { my $j = 0; for my $one ( @$row ) { push @neighbours, '0' if $wholepattern[$i-1][$j-1] eq '0'; push @neighbours, '0' if $wholepattern[$i-1][$j ] eq '0'; push @neighbours, '0' if $wholepattern[$i-1][$j+1] eq '0'; push @neighbours, '0' if $wholepattern[$i ][$j-1] eq '0'; push @neighbours, '0' if $wholepattern[$i ][$j ] eq '0'; push @neighbours, '0' if $wholepattern[$i ][$j+1] eq '0'; push @neighbours, '0' if $wholepattern[$i+1][$j-1] eq '0'; push @neighbours, '0' if $wholepattern[$i+1][$j ] eq '0'; push @neighbours, '0' if $wholepattern[$i+1][$j+1] eq '0'; my $neighbours = @neighbours; $wholepattern2[$i][$j] = ' ' if $neighbours < 2; do{} if $neighbours == 2; $wholepattern2[$i][$j] = '0' if $neighbours == 3; $wholepattern2[$i][$j] = ' ' if $neighbours > 3; @neighbours = (); $j++; } $i++; } print "t:$t"; my $k=0; for my $row (@wholepattern2) { my $l = 0; for my $one (@$row) { if( $wholepattern2[$k][$l] eq '0') { printf "0|"; } else { printf " |"; } $l++; if( $l == 39 ) { print ""; } } $k++; } printf "\n"; $t++; select(undef, undef, undef, 0.4); @wholepattern = @wholepattern2; }

        Hint: (assuming you bothered to read down this far), What happens if you try to address element $wholepattern[ -1 ][ -1 ]>?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: My Conway's Game Of Life Attempt
by eric256 (Parson) on Jan 04, 2010 at 17:18 UTC

    Hey i was reading your post and thought that there might be a clearer way to save that data so i came up with the following. I started with your code then adapted it slowly till it became this. I used strings to hold the rows instead of arrays just because it made printing and inputing a lot easier. It also iterates on key press instead of using a counter, and stops when you enter 'q'.

    #!/usr/bin/perl use strict; use warnings; use Clone qw( clone ); $|++; my $width = 29; my $height = 29; my $world = [ #0 1 2 3 #123456789012345678901234567890 ' ',#1 ' 00 ',#2 ' 0 ',#3 ' 0 ',#4 ' 00 ',#5 ' ',#6 ' ',#7 ' ',#8 ' ',#9 ' ',#10 ' ',#1 ' 000 ',#2 ' ',#3 ' ',#4 ' ',#5 ' ',#6 ' ',#7 ' ',#8 ' ',#9 ' ',#20 ' ',#1 ' ',#2 ' ',#3 ' ',#4 ' ',#5 ' ',#6 ' ',#7 ' ',#8 ' ',#9 ' ',#30 ] ; my $i = 0; print_world($world); while (<>) { chomp; exit if $_ eq 'q'; print "iteration:", $i++, "\n"; $world = iterate($world); print_world($world); } sub print_world { my $world = shift; for my $row (@$world) { print "$row\n"; } }; sub iterate { my $world = shift; my $new_world = clone($world); for my $x (0 .. $width) { for my $y (0 .. $height) { my $n = 0; for my $xc (-1,0,1) { for my $yc ( -1,0,1) { next if $xc == 0 and $yc == 0; my $tx = $x+$xc; next if $tx > $width or $tx < 0; my $ty = $y + $yc; next if $ty > $height or $ty < 0; #warn "$x + $xc = $tx\t$y + $yc = $ty"; my $tmp = substr($world->[$ty], $tx,1); $n++ if $tmp eq '0'; } } substr($new_world->[$y], $x,1) = ' ' if $n < 2 or $n > 3; substr($new_world->[$y], $x,1) = '0' if $n == 3; } } return $new_world; };

    I broke out the printing and iterating out into functions so that they can be called independently and you don't have to repeat the code. Hopefully you can use this to figure out what is wrong with your version, plus it gave me a chance to play with substr which i havn't realy done before.


    ___________
    Eric Hodges
      The intent of the substr pair would be clearer as:
      substr($new_world->[$y], $x, 1) = ( $n == 3 ? '0' : ' ' );

        Wouldn't that kill the cell if its $n == 2 and its alive? Basicaly if its $n==2 then the cell remains alive or dead, whichever it was already, dies if its $n<2 or $n>3, and becomes alive if $n==3.


        ___________
        Eric Hodges
      thanks a lot, will try it out!
Re: My Conway's Game Of Life Attempt
by ikegami (Patriarch) on Jan 05, 2010 at 03:31 UTC
    my @wholepattern2 = @wholepattern; doesn't do much of anything. @wholepattern contains references, so that's all you're copying. The actual data is in the arrays referenced by those references, and you didn't copy those.

    dclone in Storable is a tool to make a deep copy.

      thanks a lot, that's exactly the problem I was facing. fixed it now with the use of clone-any. here my final version:
      #8. attempt Game of Life 79x39 #Needs Clone-Any use strict; use Clone::Any qw(clone); my @wholepattern = ( ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','0','0','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','0','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','0','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','0','0','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','0','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','0','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','0','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','0','0','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','0','0','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'], ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',' +.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','. +','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.' +,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.', +'.','.','.','.','.','.','.','.','.'] ); #new wholepattern2 my @neighbours2; my @wholepattern2 = @{ clone(\@wholepattern) }; print "iterations: "; my $z = <stdin>; $z--; my $t = 0; my $row = (); my $one = (); my @neighbours = (); print "t:$t"; print "\n"; $t++; my $i=0; foreach $row (@wholepattern) { my $j=0; foreach $one (@$row) { if ($wholepattern[$i][$j] eq '0') { print "*"; } else { print "."; } $j++; if ($j == 78) { print "\n"; } } $i++; } print "\n"; select(undef, undef, undef, 1); while ($t <= $z) { my $i=0; foreach $row (@wholepattern) { my $j=0; foreach $one (@$row) { my $a1 = $i - 1; my $a2 = $i; my $a3 = $i + 1; my $b1 = $j - 1; my $b2 = $j; my $b3 = $j + 1; if ($a1 == -1) { my $a1 = 78; } if ($a3 == 79) { my $a3 = 0; } if ($b1 == -1) { my $b1 = 78; } if ($b3 == 79) { my $b3 = 0; } if ($wholepattern[$a1][$b1] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$a1][$b2] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$a1][$b3] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$a2][$b1] eq '0') { push (@neighbours, '0'); } #if ($wholepattern[$a2][$b2] eq '0') # { # push (@neighbours, '0'); # } if ($wholepattern[$a2][$b3] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$a3][$b1] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$a3][$b2] eq '0') { push (@neighbours, '0'); } if ($wholepattern[$a3][$b3] eq '0') { push (@neighbours, '0'); } my $neighbours = @neighbours; if ($neighbours < 2) { $wholepattern2[$a2][$b2] = '.'; } if ($neighbours == 2) { if ($wholepattern[$a2][$b2] eq '.') { $wholepattern2[$a2][$b2] = '.'; } if ($wholepattern[$a2][$b2] eq '0') { $wholepattern2[$a2][$b2] = '0'; } } if ($neighbours == 3) { $wholepattern2[$a2][$b2] = '0'; } if ($neighbours > 3) { $wholepattern2[$a2][$b2] = '.'; } @neighbours = (); $neighbours = (); $j++; } $i++; } #displaying print "t:$t"; print "\n"; my $k=0; foreach $row (@wholepattern2) { my $l=0; foreach $one (@$row){ if ($wholepattern2[$k][$l] eq '0') { #print $wholepattern2[$k][$l]; print "*"; } else { print "."; } $l++; if ($l == 78) { print "\n"; } } $k++; } print "\n"; $t++; select(undef, undef, undef, 0.2); @wholepattern = (); @wholepattern = @{ clone(\@wholepattern2) }; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://815342]
Approved by Old_Gray_Bear
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found