Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Control Statement Exercises

by vroom (His Eminence)
on Nov 24, 1999 at 00:40 UTC ( [id://1013]=perlmeditation: print w/replies, xml ) Need Help??

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"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found