#!/usr/bin/perl
use strict;
use warnings;
my ($fh,$href,$aref,$group,$array);
open($fh,"</tmp/kinput");
$href = {};
while (<$fh>) {
$aref = [split(/,/,$_)];
push(@{$$href{$$aref[0]}},$aref);
}
while ( ($group,$array) = each(%$href) ) {
$aref = $$array[rand($#$array)];
print join(",",@$aref);
}
You can reduce memory consumption by working on a "set" at a time versus building a massive hash, but you'll need to define your sets beforehand and use grep or something else to get lines from the set into the hash. This will of course drive cpu usage up.