Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PERL TUTORIALS > ROAD TO BETTER PROGRAMMING: CHAPTER 3. LOOPS, CLEAN CODE, AND THE PERL IDIOMS


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Road to better programming: Chapter 3. Loops, clean code, and the Perl idioms
By Teodor Zlatanov - 2004-02-16 Page:  1 2 3 4 5

Exercises and Resources

  1. Write a loop that counts from 1 to 100, and
    • Prints out all even numbers
    • Prints out all odd numbers
    • Prints out all the numbers that end in 1, 2, or 7
  2. Write a loop that counts down from 100 to 1

  3. Write a program that reads standard input and prints out any lines that begin with "hello" in any case (uppercase, lowercase, or mixed case). Hint: look at the "perldoc perlrun" manual page, and try to use Perl's built-in switches instead of writing your own loops.

  4. Write a logical condition that will check if a scalar is defined, non-zero, a palindrome (the same backwards and forward), or the number 234.98. Test with and without the warnings "-w" flag to the Perl interpreter and the "use strict" compiler pragma. Do you understand the warnings that were generated, if any?

  5. Write the following in normal (non-postfix) notation, and simplify them if you can:

    • print if $debug;
    • $i++ unless $i > 10;
    • unless ($j && !$i) { $j += $i while <> };
    • next while <>;

Resources



View Road to better programming: Chapter 3. Loops, clean code, and the Perl idioms Discussion

Page:  1 2 3 4 5 Next Page: Perl loops

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.