You can do that by shuffling an array of names and coupling neighbors in the result. I've omitted the file I/O parts for clarity.
This imports an excellent shuffle() function, use List::Util 'shuffle'; This reads an array full of the names with numbers and dot stripped, and then strips newlines, too,
Shuffle them and stuff them into a hash, which naturally pairs them,my @friends = map { (split /\./)[1] } <DATA>; chomp @friends;
Finally, print the pairs,@friends = shuffle @friends; my %couple = (@friends);
Here's the "file" that code reads. To read from a named file, instead, just do as you did above.for (keys %couple) { print "$_ and $couple{$_} are partners\n"; }
__DATA__ 1.bobby 2.jane 3.charleen 4.markus 5.gabriel 6.Alex
Perl data structures and the shuffle function make this a lot easier.
After Compline,
Zaxo
In reply to Re: Random Couple Script
by Zaxo
in thread Random Couple Script
by gsr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |