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

Control Statement Exercises

  1. Write a program which reads lines until it reads a line consisting of the word "quit" on a line by itself. Then print out all the duplicate lines seen. If none were seen, print "No duplicates seen." (solution)
  2. Write a program which prints all the numbers from 1 to 100, each followed by a ":" and a list of the numbers by which it is evenly divisible. (solution)

Replies are listed 'Best First'.
Control Statements #1
by vroom (His Eminence) on Nov 24, 1999 at 01:15 UTC
    # Reads in lines of text until "quit" is read. # Then prints duplicates, or prints "No duplicates seen." my %lines; # holds lines of text encountered along with a count # of how many times it has been seen my @dups; while ( $_ = <> and $_ ne "quit\n" ) { $lines{$_}++; # increment value. if it dosn't exist yet, initializ +es to 1 if ( $lines{$_} == 2 ) # been seen before so add to dupes list { push @dups, $_; } } if ( @dups ) { print "Here are your duplicate lines:\n\n"; foreach ( @dupes ) { print; } } else { print "No duplicates seen.\n"; }
Control Statements #2
by vroom (His Eminence) on Nov 24, 1999 at 01:26 UTC
    # prints out all the numbers from 1 to 100, along with any # numbers they're evenly divisible by for ( my $i = 1; $i <= 100; $i++ ) { print "$i: "; for ( my $j = 1; $j <= $i; $j++ ) { if ( $i % $j == 0 ) { print "$j "; } } print "\n"; }