Dave Cross writes:

>I'm tidying up a script that splits a file into several other files.
>The script is called like this:
>
>splitter.pl < input.dat 3> out1.dat 4> out2.dat 5> out3.dat

Try something like


#!/usr/bin/perl

use warnings;
use strict;

use IO::Handle;
my @fh;

for my $num (3..5) {
        $fh[$num] = new IO::Handle;
        $fh[$num]->fdopen($num, "w")
            or die "can't open fd $num for writing: $!\n";
}

while (<DATA>) {
        my ($num, $text) = split /\s+/, $_, 2;
        next unless $fh[$num];
        print {$fh[$num]} $text;
}

__DATA__
3 one
3 two
3 three
4 four
4 five
5 six
3 seven
5 eight
4 nine


Marcel

-- 
We are Perl. Your table will be assimilated. Your waiter will adapt to
service us. Surrender your beer. Resistance is futile.
 -- London.pm strategy aka "embrace and extend" aka "mark and sweep"

Reply via email to