#!/usr/bin/perl srand(); # initiate random generator for rand() command $totalline = 0; # var for hold total number of lines (names); starts it at 0 if (!$ARGV[0] || !$ARGV[1]) { &usage; } else { &openread; &compile_list; &makegroups; } # Usage Subroutine. Displayed if program not used properly sub usage { print "Random Couple Generator\n"; print "\n Usage \n"; print "./random-couples.pl name-file target-file\n"; print "Example: ./random-couples friends couples\n"; print "\t Where friends is your list of friends, and couples is the new list of couples\n\n"; } # opens specifed read file, and processes it to be parsed sub openread { open (READ, "< $ARGV[0]") or die "Cant open $ARGV[0] : $!"; while() { s/#.*//; # ignore comments (remove them) next if /^(\s)*$/; # skip blank lines s/\d//g; # remove any numbers in the text file s/\.//g; # remove periods chomp; # remove newline (\n) push @file_contents, $_;# push line into array } } # calculates how many lines are in the file sub randomize { while (@file_contents) { $totalline++; # find total number of names } } # create array with random names sub compile_list { foreach $names (@file_contents) { &randomize; # find total number of names $ng = int(rand($totalline)); # random number from 0 to $totalline, made into an integer for array $finalizedlist[$tmp] = $file_contents[$ng]; # set finalizedlist element $tmp to random number splice(@file_contents, $ng, $ng); # take person out $tmp++ # goto next value of finalizedlist array } } # open write file, write two random people, close write file sub makegroups { $fark = 0; $fark2 = $fark + 1; while ($fark2 != $totalline) { open (WRITE, ">> $ARGV[1]") or die "Can\'t open $ARGV[1] : $!"; print WRITE "Group $fark: ".$finalizedlist[$fark]." and ".$finalizedlist[$fark2]." are partners\n"; close WRITE; $fark++; $fark2 = $fark + 1; } } #### 1.bobby 2.jane 3.charleen 4.markus 5.gabriel 6.Alex #### Group 0: Alex and are partners Group 1: and are partners Group 2: and are partners Group 3: and bobby are partners Group 4: bobby and are partners Group 5: and are partners and then it goes like Group 5, until it reaches 24.