This is a simple sudoku solver (if written unformatted, it's about 7x80-char lines of code). I'm a beginner in Perl, and this looked like a good first program, but of course there are surely many naiveties in the code. Any criticism/remark/comments welcome.

use strict; use warnings;my @r; sub r{my $l=shift;my%x=(); for(0..8){$x{int($l/9)*9+$_}="" ;$x{($l%9)+($_*9)}="";}my$k=int($l /27)*27+int( ($l%9)/3)*3; @x{$k..$k+2, $k+9..$k+11, $k+18..$k+20 }=("")x9; delete $x{$l}; return %x;}sub v{for(0..80){ next if($_[$_ ]eq".");return 0 if (join("", @_[@{$r[$_]}]) =~/$_[$_]/) } return 1}sub z{ my($p,@s)= (shift,@_); v( @s)||return 0; print"Cur: ", @s;for(;$s[$p ]ne".";$p++){ return print"end: ",@s if($p==81); }for(1..9){$s[$p]=$_;return 1 if (z($p+1,@s));}}for(0..80){my %r=r($_);$r[$_]=[keys %r]; };z(0,split(//,<>));

To use it, just give it the 81-chars string representing the board on stdin, eg

echo ".21.3.74.9.8...1 (snip) 3.9.65.1.42." | sudoku.pl


In reply to Sudoku solver by waldner

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.