in reply to Split up file depending unique values 1st column

It sounds like you want something as simple as:
use warnings; use strict; my $file = shift; open my $IN, '<', $file or die "Cannot open '$file' $!"; while ( <$IN> ) { my ( $field ) = /^([^,]+)/ or die "Error: field not found.\n"; open my $OUT, '>>', $field or die "Cannot open '$field' $!"; print $OUT $_; } close $IN;

Replies are listed 'Best First'.
Re^2: Split up file depending unique values 1st column
by GertMT (Hermit) on Dec 17, 2006 at 12:54 UTC
    This works really well! I can't say I didn't expect shorter code. Though it is shorter I'll definitely need to take some time to try to figure out what's going on.
    Thanks again, Gert
      If you really want shorter code then:
      use warnings; use strict; /^([^,]+)/ && open F, ">>$1" and print F while <>; __END__
      :-) The way it works is that it opens a file in append mode for every line that matches the regular expression using the first field as the file name and prints the current line to the end of that file.

        awesome!
        And with the explanation I even do understand the grammar of this one (line).