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.

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; }
and then you can use grep - this: if ((!&test2($xCur, $yCur, $wall) && !&test3($xCur, $yCur, $wall) && &test5($xCur, $yCur, $wall) && &test6($xCur, $yCur, $wall) && !&test9($xCur, $yCur, $wall))&& (!&test1($xCur, $yCur, $wall) &&  !&test7($xCur, $yCur, $wall) ) || (&test1($xCur, $yCur, $wall) && &test4($xCur, $yCur, $wall) && &test7($xCur, $yCur, $wall) && !&test8($xCur, $yCur, $wall))) becomes if(grep { check_coord($xCur, $yCur, $wall, $_) } 2..8) {

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.


In reply to Re: large expression formating by Aristotle
in thread large expression formating by Dr.Altaica

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.