gsr has asked for the wisdom of the Perl Monks concerning the following question:
When I execute the code on a text file containing: File: names#!/usr/bin/perl srand(); # initiate random generator for rand() command $totalline = 0; # var for hold total number of lines (names); start +s 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(<READ>) { 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 finalized +list element $tmp to random number splice(@file_contents, $ng, $ng); # take person out $tmp++ # goto next value of finalizedli +st 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 ".$fin +alizedlist[$fark2]." are partners\n"; close WRITE; $fark++; $fark2 = $fark + 1; } }
with the command perl random-couples.pl names results then the results file is filled with crap: File: Results1.bobby 2.jane 3.charleen 4.markus 5.gabriel 6.Alex
any help or suggestions are much appreciatedGroup 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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Random Couple Script
by Zaxo (Archbishop) on Nov 09, 2004 at 03:38 UTC | |
|
Re: Random Couple Script
by tachyon (Chancellor) on Nov 09, 2004 at 03:48 UTC | |
|
Re: Random Couple Script
by Roy Johnson (Monsignor) on Nov 09, 2004 at 03:43 UTC | |
|
Re: Random Couple Script
by BrowserUk (Patriarch) on Nov 09, 2004 at 04:30 UTC | |
by thospel (Hermit) on Nov 09, 2004 at 12:02 UTC | |
by Roy Johnson (Monsignor) on Nov 09, 2004 at 15:05 UTC | |
|
Re: Random Couple Script
by jethro (Monsignor) on Nov 09, 2004 at 14:36 UTC | |
|
Re: Random Couple Script
by thospel (Hermit) on Nov 09, 2004 at 11:39 UTC | |
|
Re: Random Couple Script
by TedPride (Priest) on Nov 09, 2004 at 08:50 UTC |