http://qs321.pair.com?node_id=1056839


in reply to Chess Board Single Loop

Step aside everyone. Someone who took a BASIC programming class at a local college in the mid 80s coming through....

If this is an "old school" procedural programming course, then the answer would typically be something like this:

#!/usr/bin/env perl use strict; use warnings; my ($row, $col) = (1,1); while ($row <= 8 and $col <= 8) { print (($row - 1) * 8 + $col, "\t"); if ($col == 8) { # start next row print "\n"; $row = $row + 1; $col = 1; } else { $col = $col + 1; } }

Aside from my painfully primitive code above, whatever else I wrote will now be read as derivative of davido's excellent posts so I buried it in readmore tags because it did take me down memory lane and I hadn't the heart to cull it. (In particular, I did go to the one-liner, which he held off from writing because he doesn't want to scare off beginners. Not me ... BOO! ;-)

About the above code, it ain't slick, it ain't elegant, but it does show conditionals where the logical result is divorced from the happy circumstance that the value of zero is interpreted as false. This is what my beloved teacher, Sr. Barbara Kushan, would typically expect to see.

Beginning programmers with only a few weeks experience typically aren't that far beyond the "describe how you would solve it without a computer, then see if you can have the computer do that" stage. (Doesn't some monk here have a .sig to that effect?) So the solution is formulated in terms of rows and columns. The TAB character is unrepentantly used to avoid details about printing. Lastly, enumeration is used in preference to offset indexing. (And I love modulo arithmetic, but there is seemingly no end to the number of people that tell me that modulo arithmetic violates KISS. I know, unbelievable, right?)

Ideally this program could then be transformed into a "programmer's domain" where we would base everything off of a loop variable that goes from 1..64. Also, we have the line above which prints the number based on how row numbers and column numbers combine, so using the modulo operator should not be a complete mystery when going to this fancier program.

#!/usr/bin/env perl use strict; use warnings; my $num = 1; while ( $num <= 64) { print $num; print $num % 8 ? "\t" : "\n"; $num = $num + 1; }

I think the ternary operator in perl would be confusing to a newbie, but to condense the code evolution I'm using it. The issue here is that I want to get to what I learned as a "for-next loop" instead of a while loop. It is always easier to explain to a raw beginner a finite indexed loop than a loop based on a conditional. (I started with a while loop because I wanted to satisfy the "single loop" criterion and at the same time use rows and columns. LanX has the natural nested loop solution.) Below is what I think a beginner's BASIC-flavored procedural solution would look like in perl if we allowed a sprinkling of nice perl syntax.

use strict; use warnings; for (my $x = 1; $x <= 64; $x += 1) { print $x, $x % 8 ? "\t" : "\n"; }

And I can't resist the natural one-liner, which makes things a little too compact for a beginner, but I don't think it's that obscure:

perl -e 'print $_, $_ % 8 ? "\t" : "\n" for 1..64;'

Thanks for taking me back to the days of the Trash-80, Apple II and PDP-11. According to Dijkstra it ruined me for life, but it was fun. & still is ;-)