in reply to Split file based on field

my $temp; my $header = <DATA>; while( <DATA> ){ my ($id, $data) = /(\S+)(.+)/; open my $fh, '>>', $id or die "cannot open file $id: $!"; print $fh $header if $temp ne $id; print $fh $id, $data, $/; $temp = $id; } __DATA__ Usr1369***12556 06-01-0101:00 1169 <snipped off> 06-01-0101:00 2396 <snipped off> 06-01-0103:12 1169 <snipped off> 06-01-0103:12 2569 <snipped off> 06-01-0301:00 1169 <snipped off> 06-01-0301:00 2396 <snipped off>


Replies are listed 'Best First'.
Re^2: Split file based on field
by holli (Abbot) on Jan 10, 2006 at 11:10 UTC
    Your solution has some flaws. It
    • breaks under windows, because you try to use a ":" in a filename
    • assumes the input is sorted and breaks if the is input is not sorted.
    • opens/closes the file(s) for every! line of input data


    holli, /regexed monk/
          # breaks under windows, because you try to use a ":" in a filename
    • ":" can be changed
          assumes the input is sorted and breaks if the is input is not sorted.
    • yes, it is sorted
          opens/closes the file(s) for every! line of input data
    • yes, I can see