Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Perl as a teaching aid

by spacewarp (Pilgrim)
on Jun 23, 2001 at 09:21 UTC ( #90932=CUFP: print w/replies, xml ) Need Help??

Here's a quick program that I threw together for a teacher friend, with three intended purposes:

1. Provide a working ASCII to binary translator as a teaching aid.
2. Show that the algorithm taught in-class actually works in practice.
3. Demonstrate, programming-wise, the implementation of a known algorithm.

for (;;) { print "Enter a character or word: "; chomp($input = <STDIN>); exit unless $input; @chars = split ('',$input); foreach ( @chars ) { $dec = unpack("c",$_); if ($dec >= 128) {$dec -= 128; $bin[0] = 1} else {$bin[0] = 0;} if ($dec >= 64) {$dec -= 64; $bin[1] = 1} else {$bin[1] = 0;} if ($dec >= 32) {$dec -= 32; $bin[2] = 1} else {$bin[2] = 0;} if ($dec >= 16) {$dec -= 16; $bin[3] = 1} else {$bin[3] = 0;} if ($dec >= 8) {$dec -= 8; $bin[4] = 1} else {$bin[4] = 0;} if ($dec >= 4) {$dec -= 4; $bin[5] = 1} else {$bin[5] = 0;} if ($dec >= 2) {$dec -= 2; $bin[6] = 1} else {$bin[6] = 0;} if ($dec == 1) {$dec -= 1; $bin[7] = 1} else {$bin[7] = 0;} print "$_: " . join('',@bin) . "\n"; } }


Use of this advanced computing technology does not imply an endorsement
of Western industrial civilization.

Replies are listed 'Best First'.
Re: Perl as a teaching aid
by mirod (Canon) on Jun 23, 2001 at 12:00 UTC

    If this is an educational program I have 2 issues with it:

    • you do not use strict or warnings: adding a couple of my here and there does not make the script more verbose but let you show students a piece of code that follow the accepted best practices in the Perl community,
    • the core of your script is the same line, repeated 8 times: this is most definitely a case where you should use a loop to simplify the code. A good part of learning programming involves learning how to extract similar operations and put them in a loop or in a subroutine, and I really think students should be exposed to it as early as possible.

    Here is an example of how you can write this:

    #!/bin/perl -w use strict; my $input; do { print "Enter a character or word: "; chomp( $input = <STDIN>); my @chars = split ('',$input); foreach ( @chars ) { my $dec = ord( $_); my @bin; for( reverse 0..7) { if( $dec >= 2**$_) { push @bin, 1; $dec -= 2**$_; } else { push @bin, 0; } } print "$_: " . join('',@bin) . "\n"; } }while( $input);

    While this is probably not the most clever, or even the clearest piece of code that could be written to solve your problem (the for (reverse 0..7) is a bit kludgy for example) I think it would be a better example to students than the original version.

      If this script is intended to teach perl, or programming in general, then i agree that there are things here the students should not learn. I also think that, in that case, the for(;;) loop should be made legible. However, i got the idea that this perl was for something else; he did, after all, say it was a teaching aid, not an aid for teaching perl.

      Spacewarp's script is much more readable for a student, i think. As such, it is better for making a point that can be easily followed about the nature of binary mathematics. If the algorithm was, in fact, presented in class as he coded it, then it is an accurate representation of the algorithm the teacher picked; since representing that algorithm was one of the stated goals, that's a good thing, even if the algorithm was bad. After all, you don't teach someone the french for "I went to the store" is "J'irai au cinéma" just because they should go to a movie instead. (This may seem like a nitpick, but i was given a few such programs to "learn" from and they drove me up a wall, because they did things totally unlike what they were said to do.)

      And, really, if we're talking about a better algorithm, you should start with whatever power of two is the first one under the target number IMO. (int (log($n)/log(2)))

      But to return to my point, i think his perl was not to teach perl, but to introduce them to the basic idea of programming. Actually, it might be a neat recurring theme for the class; take this bit of code and change it as they learn more code concepts, to include subroutines and be able to split things into different bases (ord(A) in trinary looks very different than ord(A )in binary...)

      just my take.

      thanks to OeufMayo for the french snippet.

      I never post cause im not english but, teaching perl? try this:

      #!/usr/bin/perl use strict; print "Enter a character or a word: "; chomp($_=<STDIN>); print "$_->[1]: $_->[0]\n" for map{[unpack("B*",$_),$_]}split'';
      Update: Golf time:

      perl -wle '$_=<STDIN>,chop;map{print"$_: ",unpack("B*",$_)}split//'



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://90932]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2018-03-24 18:28 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.