Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: grabbing random n rows from a file

by Anonymous Monk
on Jul 19, 2006 at 18:41 UTC ( [id://562385]=note: print w/replies, xml ) Need Help??


in reply to grabbing random n rows from a file

#!/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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://562385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-24 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found