Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: mem usage

by kejohm (Hermit)
on May 27, 2010 at 02:58 UTC ( #841867=note: print w/ replies, xml ) Need Help??


in reply to mem usage

You could try using Tie::File with a Fisher-Yates shuffle in place:

#!perl use strict; use warnings; use Tie::File; my $filename = shift; die 'No file' unless $filename; my $tie = tie my @file, 'Tie::File', $filename, memory => 20_000_000; die "Couldn't tie $filename: $!" unless $tie; $tie->defer(); shuffle(\@file); $tie->flush(); undef $tie; untie @file; sub shuffle { my $deck = shift; # $deck is a reference to an array my $i = @$deck; while (--$i) { my $j = int rand ($i+1); @$deck[$i,$j] = @$deck[$j,$i]; } } __END__

This worked well enough for me using a simple text file containing 1 million lines of random numbers.


Comment on Re: mem usage
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2015-07-02 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls