http://qs1969.pair.com?node_id=994

Basic I/O Exercises

  1. Write a program which asks the user for his/her first name and last name, and then prints out a line of text welcoming him/her using the given names. (solution)
  2. Write a program which reads lines until it reads a "." on a line by itself, then prints out all of the lines it has read in reverse order. (solution)

Replies are listed 'Best First'.
Basic I/O #2
by vroom (His Eminence) on Nov 17, 1999 at 03:06 UTC
    # Read lines until a line consisting only of "." is seen. # Then print the lines back out in the opposite order. my @lines; while ( $line = <> and $line ne ".\n" ) { push @lines, $line; } foreach ( reverse @lines ) # only reverses the list used by foreach; d +oes not modify the array { print; }
Basic I/O #1
by vroom (His Eminence) on Nov 17, 1999 at 03:02 UTC
    # prompt for and read First and Last name, then print a welcome messag +e. print "\nWhat is your first name? "; my $first = <>; print "\nWhat is your last name? "; my $last = <>; chomp $first; # strip the newline from the first name print "Welcome $first $last";