Nice code. While playing around with different values of $s, I started losing the "H". At first I thought it was the way that I had modified the code to remove trailing whitespace and empty lines from the output. I then realized that the script was running out of DATA. I added the line "seek DATA, 0, 0 if eof(DATA)" after last; And since the seek is now used more than once, I created a subroutine to do that work. Here's my updated version:
#!/usr/bin/perl -w
($s=pop)||($s=10);($p)=@m=(0,0,1,0);$/=\1;
$r = sub {seek DATA,0,0};sub t{@m[2,3]=(0,
0);for$y(split//,$_[0]){for(2,3) {$m[$_]+=
{n=>[-1,0],s=>[1,0],e=>[0,1],w=>[0,-1]}->{
$y}->[$_-2];}}}$SIG{__WARN__}=sub{$p=!$p};
&$r;for (qw( 6 se p 2 e 2 ne
2 n 5 p e 2 s
8 p n 6 ne 2 e
1 se 2 s 5 n 2 w 4 e 4
p e 2 s 3 p
n 8 e 4 se 1
s 2 sw 1 w 4 p
se 4 e 3 p n 8
s 4 e 4 n 4 s
7
)) {$_[0]=$_;tr/nsew//?&t:&f}sub f{for(1..
$_[0]*$s){for(0,1){$m[$_]+=$m[$_+2]}if ($p
){while(<DATA>){$t=$_;tr/\n\t //||last;&$r
if eof(DATA);}}$g[$m[0]][$m[1]]=$p?$t:" ";
}}for$i(0..($s*20)){$l = "";for(0..($s*30)
){$l.=$g[$i][$_]||" ";}$l=~ s/\s+$//;print
"$l\n" if length($l);}
__END__
You can also pass $s in and the ranges aroung the prints should scale properly.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.