I am trying to create a perl script, that reads a list of names from a text file, and randomly couples them. So, it must be random, and after a name is used once, it cannot be used again. Here is the code I have written: File: random-couples.pl
#!/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; } }
When I execute the code on a text file containing: File: names
1.bobby 2.jane 3.charleen 4.markus 5.gabriel 6.Alex
with the command perl random-couples.pl names results then the results file is filled with crap: File: Results
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.
any help or suggestions are much appreciated

In reply to Random Couple Script by gsr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.