in reply to large expression formating
You have false laziness and not enough hubris. (Which means you are copy-pasting where you should be abstracting.)
The fact you are calling your subs test1, test2, test3 etc should immediately set off the alarms.
As a minor note, whenever you want to return "false" from a function in Perl, you should do that with a blank return. You should neither return 0 nor return "" - because these return a one-element list, which while false in scalar context is true in list context. A blank return will return undef in scalar and an empty list in list context, which is what you want.
and then you can use grep -my @displacement = ( [-1, -1], [-1, 0], [-1, +1], [ 0, -1], [ 0, 0], [ 0, +1], [+1, -1], [+1, 0], [+1, +1], [-1, -1], ); sub check_coord { my ($x, $y, $wall, $coord)= @_; for($displacement[$coord - 1]) { $x += $_->[0]; $y += $_->[1]; } return if $y < 0 or $x < 0 or not defined( $map[$y] or $map[$y][$x] ); return $map[$y][$x] eq $wall; }
since grep in scalar context returns the number of matches.
You can apply the same translation to the rest of your code.
I recommend you read Mark-Jason Dominus' excellent Program Repair Shop and Red Flags article series on Perl.com.
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: large expression formating
by Dr.Altaica (Scribe) on Oct 20, 2002 at 22:42 UTC | |
by Aristotle (Chancellor) on Oct 20, 2002 at 23:41 UTC |