#!/usr/bin/perl -w

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  
# final_3.pl
# 
# assignment: write a program that asks for five words and prints out
#             all possible combinations of those five words
#           
#
# Author: John Berry
# Date: 12/11/03
#
# Version 0.01
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  

use strict;

my $VERSION = "0.01";

my @words;                   # word list
my $input;                   # user input line
my $valid_list = 0;          # boolean value to control while loop

# while we don't have a list of exactly five words
while(!$valid_list) {

  # prompt for, and accept, user input
  print("What are the five words? ");
  $input = <STDIN>;
  chomp($input);

  # split the user input up into the words
  @words = split(/ /, $input);


  # if there are NOT exactly 5 words, report the problem
  if(scalar(@words) != 5) {
    print("Oops, you entered ", scalar(@words), " words.  Please try again\n");

  # if there ARE 5 words set our valid list boolean
  } else {
    $valid_list = 1;
  }
}


# loop through the words and print out all combinations without repeats
foreach my $outer_word (@words) {
  foreach my $inner_word (@words) {
    if($outer_word ne $inner_word) {
      print "$outer_word $inner_word\n";
    }
  }
}
