Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Chess Board Single Loop

by xantithor (Novice)
on Oct 03, 2013 at 22:44 UTC ( [id://1056809]=note: print w/replies, xml ) Need Help??


in reply to Re: Chess Board Single Loop
in thread Chess Board Single Loop

I created an array for her, but she was becoming more confused the longer I spoke about it. Apparently she is taking a class at the local community college, and she hasn't gotten to learning about array's just yet. I'm attempting to simplify it for her by using a for_loop, and for some unknown reason I am stumped on this. The array came natural, but I feel like Ol' McDonald from the Geico commercials.

Replies are listed 'Best First'.
Re^3: Chess Board Single Loop
by xantithor (Novice) on Oct 03, 2013 at 22:59 UTC
    This is probably what will help her most, but how do I make it stop at 8, and start at the next row?
    #!/usr/bin/perl -w use strict; print "$_\n" foreach ('1'..'64');

      I'm trying to keep this in terms that someone in an introductory CS class would be equipped to grasp. Therefore, I'm not using an array, Perl's higher-order functions, etc. And as you requested, I'm using a single loop; no hidden implicit ones, just a single flat loop. The modulus operator should be taught in the first week or two of any intro to CS course.

      foreach ( 1 .. 64 ) { print "\n" if ( $_ - 1 ) % 8 == 0; print "$_ "; }

      The foreach loop iterates over the numbers 1 through 64, inclusive. On each iteration, the variable "$_" holds the value for a single integer within the range of 1 through 64.

      First, we test whether "number minus one, mod eight" is zero. In other words, using integer math, whether the number minus one, divided by eight, has no remainder. If there is no remainder, print a newline character.

      Then proceed to print the number itself.

      Then iterate again.

      Actually the formatting would be a little cleaner if you test for a remainder of 7, and in that case print a newline after the number:

      foreach ( 1 .. 64 ) { print "$_ "; print "\n" if ( $_ - 1 ) % 8 == 7; }

      For purists, there is a hidden loop, but it's not a part of our algorithm; constructing the range 1 .. 64 to iterate over is done by Perl using an internal loop. If we really wanted to keep it pure, we could use "while", or a C-style for.


      Dave

        Dave, You found what I was looking for. I got out of touch with some of the simpler things, and I stared at the more complicated details. I need to sit down and refresh my memory on these. Thanks for the assist with this one.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found