Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Shuffle flat file

by JimJx (Beadle)
on May 20, 2003 at 06:14 UTC ( [id://259371]=perlquestion: print w/replies, xml ) Need Help??

JimJx has asked for the wisdom of the Perl Monks concerning the following question:

I hope someone has a quick answer to this.

I have flat file that I am using as a small DB, has about 50 entries max like this:
Entry1:Desc:Keyword1,Keyword2,Keyword3.....
Entry2:Desc:Keyword1,Keyword2,Keyword3.....

My problem is I need to shuffle the file based on the first field. I tried the Fisher-Yates but just could not get it to work, I didn't get any output at all.....

So, if anyone has any info to help me out with this, it would be greatly appreciated.

I am kinda new to PERL to please take it easy on me. :-)
Jim

Replies are listed 'Best First'.
Re: Shuffle flat file
by Enlil (Parson) on May 20, 2003 at 06:24 UTC
    There is no shame in posting code (we were all Perl newbies once). We can't know what is wrong (and hence why it won't work) without seeing some code.

    I don't understand what you mean by shuffling based on first field, a random shuffle is a random shuffle. Anyhow, here is some code to help you get started (the code for the shuffle is the same as in the docs (ie. perldoc -q random):

    use strict; use warnings; my @array = <DATA>; fisher_yates_shuffle(\@array); print @array; sub fisher_yates_shuffle { my $deck = shift; # $deck is a reference to an ar my $i = @$deck; while ($i--) { my $j = int rand ($i+1); @$deck[$i,$j] = @$deck[$j,$i]; } } __DATA__ Entry1:Desc:Keyword1,Keyword2,Keyword3 Entry2:Desc:Keyword1,Keyword2,Keyword3 Entry3:Desc:Keyword1,Keyword2,Keyword3 Entry4:Desc:Keyword1,Keyword2,Keyword3 Entry5:Desc:Keyword1,Keyword2,Keyword3 Entry6:Desc:Keyword1,Keyword2,Keyword3 Entry7:Desc:Keyword1,Keyword2,Keyword3 Entry8:Desc:Keyword1,Keyword2,Keyword3 Entry9:Desc:Keyword1,Keyword2,Keyword3 Entry10:Desc:Keyword1,Keyword2,Keyword3
    -enlil
(jeffa) Re: Shuffle flat file
by jeffa (Bishop) on May 20, 2003 at 06:34 UTC
    Shuffle based on the first field? Why not just shuffle the entire file? Here is a very consise way of doing so using a couple of useful CPAN modules, Tie::File so we can access the file as an array and List::Util which contains a shuffle sub (which is a Fisher-Yates implementation, i believe) ready to go:
    use strict; use warnings; use Tie::File; use List::Util qw(shuffle); my @file; tie @file, 'Tie::File', 'foo.dat' or die $!; @file = shuffle(@file);

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Shuffle flat file
by Zaxo (Archbishop) on May 20, 2003 at 06:20 UTC

    What do you mean by a shuffle 'based on the first field'? I'd think in terms of shuffling whole records, and a random ordering doesn't usually depend on values.

    Given the file size you mention, slurping to an array and hitting it with Fisher-Yates sounds like the right way to do it. What goes wrong with your attempt?

    After Compline,
    Zaxo

Re: Shuffle flat file
by chromatic (Archbishop) on May 20, 2003 at 06:21 UTC

    Can you provide a small code snippet that you've tried already? I'm not sure what you mean by "shuffle the file based on the first field", and it's tough to guess what kind of output you want.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found