Randomise splitting file into two new files

Discussion in 'Perl Misc' started by Bumble, Jan 27, 2004.

  1. Bumble

    Bumble Guest

    Hey all! Basically I require a program that will take a file full records (1
    per line) and then output these lines into 2 new files, randomly. Anyone
    know of such a program already in existence?

    --
    Bumble

    "I'm flat out, you're so beautiful to look at when you cry. Freeze don't
    move, you've been chosen as an extra in the movie adaptation of the sequel
    to your life. Shady Lane, everyone wants one, Shady Lane, everybody needs
    one" - Pavement
    Bumble, Jan 27, 2004
    #1
    1. Advertising

  2. Bumble

    Guest

    "Bumble" <> wrote:
    > Hey all! Basically I require a program that will take a file full records
    > (1 per line) and then output these lines into 2 new files, randomly.
    > Anyone know of such a program already in existence?


    open F1, ">", pop or die $!;
    open F2, ">", pop or die $!;
    while (<>) {
    if (rand()<0.5) {
    print F1 $_;
    } else {
    print F2 $_;
    };
    };
    close F1 or die $!;
    close F2 or die $!;

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    , Jan 27, 2004
    #2
    1. Advertising

  3. Bumble

    Bumble Guest

    wrote:
    >
    > open F1, ">", pop or die $!;
    > open F2, ">", pop or die $!;
    > while (<>) {
    > if (rand()<0.5) {
    > print F1 $_;
    > } else {
    > print F2 $_;
    > };
    > };
    > close F1 or die $!;
    > close F2 or die $!;
    >
    > Xho


    Many thanks! Didn't realise it was so straight forward!! It's working great.

    --
    Bumble
    http://bumble.rumble.at
    http://www.cossar.co.uk

    "I'm flat out, you're so beautiful to look at when you cry. Freeze
    don't move, you've been chosen as an extra in the movie adaptation of
    the sequel to your life. Shady Lane, everyone wants one, Shady Lane,
    everybody needs one" - Pavement
    Bumble, Jan 27, 2004
    #3
  4. Bumble wrote:
    >
    > Hey all! Basically I require a program that will take a file full records (1
    > per line) and then output these lines into 2 new files, randomly. Anyone
    > know of such a program already in existence?


    my @fh;
    open $fh[ $_ ], '>', "test44_$_.txt" or die $! for 0, 1;
    print { $fh[ rand @fh ] } $_ while <>;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Jan 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Ericson
    Replies:
    0
    Views:
    419
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    433
  3. John Dibling
    Replies:
    0
    Views:
    405
    John Dibling
    Jul 19, 2003
  4. jsaul
    Replies:
    7
    Views:
    322
    Peter Abel
    Apr 2, 2004
  5. Raymond Hettinger

    splitting one dictionary into two

    Raymond Hettinger, Apr 2, 2004, in forum: Python
    Replies:
    1
    Views:
    291
    wes weston
    Apr 2, 2004
Loading...

Share This Page