Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Chess Board Single Loop

by marinersk (Priest)
on Oct 03, 2013 at 22:54 UTC ( [id://1056812]=note: print w/replies, xml ) Need Help??


in reply to Chess Board Single Loop

While it can be done with a single loop, it seems to me you have to use less intuitive coding techniques to enforce that arbitrary constraint.

I'm beyond understanding why not just use a nested loop and be done with it.

#!/usr/bin/perl -w use strict; foreach my $y_axis (1..8) { my $output_text = ''; foreach my $x_axis (1..8) { $output_text .= " $x_axis$y_axis "; } print "$output_text\n"; } exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-10-03@1652-8x8-Loop>8x8.pl 11 21 31 41 51 61 71 81 12 22 32 42 52 62 72 82 13 23 33 43 53 63 73 83 14 24 34 44 54 64 74 84 15 25 35 45 55 65 75 85 16 26 36 46 56 66 76 86 17 27 37 47 57 67 77 87 18 28 38 48 58 68 78 88

That said, BrowserUK produced, as usual, a brilliant one-loop solution.

Replies are listed 'Best First'.
Re^2: Chess Board Single Loop
by davido (Cardinal) on Oct 03, 2013 at 23:39 UTC

    In week one or two of intro to CS at a community college level, students may not have been introduced to the concept of arrays, nested loops, unpack templates, map, and so on. However, anyone with grade-school math in their past understands remainders with integer division (ie, the modulus operator). What's amazing is that such a simple thing, taught in 4th grade, is useful to low-level-language programmers even today when using arithmetic to simulate multi-dimensional structures in flat memory.


    Dave

      Ironically, all my math teachers in elementary school glossed over the chapters which covered other bases (generally base 12 for the clock and we were done) and completely skipped over modulo operations.

      I first learned about the concept of "modulo" in my first programming course in high school -- and that was taught after nested loops.

      It's funny how we keep having to learn that the rest of the world didn't have our life experiences; they had their own. To me, nested loops seems like the third thing you learn in programming; to you, knowing modulo seems like a 4th grade thing.

      Excellent post, davdio, as always.

      Update: After posting this, I see a metric ton of the same approach being done, including re-reading one of your prior posts, davido, and realize that the code snippet was unnessesary.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1056812]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found