On Wed, 28 Nov 2001, Ian Phillipps wrote:

> On Tue, 27 Nov 2001 at 11:31:39 -0600, Andy Bach wrote:
> > Okay, would the maths folk like to offer a helpful explanation? 
> > ---------- Forwarded message ----------
> > How do I select a random line from a file?
> 
> See also
> http://groups.google.com/groups?threadm=xn9zo7tvb5h.fsf%40owl.doc.ic.ac.uk

Tada! Got it, finally - though my denseness partially hinged on 
misreading the "significant advantage in space over reading the whole 
file" addendum - you still read the whole file, but only have one line at 
at time (or however its handled) in mem.  I was thinking it somehow 
short-circuited the while once it succeeded (being < 1).  

Thanks much.  I thought there might be related technique for getting a 
rand element from an array, but we know the array length so ... this 
(from above) did help w/ my getting all the elements in a random order, i.e.:
while ( @arr ) {
      $pos = rand($#arr);
      $val = splice @arr, $pos, 1;
      print "is: $val\n";
}

Such fun!

a

Andy Bach, Sys. Mangler             
Internet: [EMAIL PROTECTED]    VOICE: (608) 264-5178 ex 5738, FAX 264-510

   "Murphy's Law, that brash proletarian restatement of Godel's Theorem ..."
                -- Thomas Pynchon, "Gravity's Rainbow"



Reply via email to