in reply to Grouping unique lines into a file.
Assuming the above is correct, here is how I would do it:
Here is how it would look if you know you will be safe caching file handles.#!/usr/bin/perl use strict; use warnings; my %seen; while (<DATA>) { chomp; my ($acct, $data) = $_ =~ m{^(\d\d)(.*)}; next if $seen{$acct}{$data}++; append_data($acct, $_); } # If you know you are not going to exceed the open filehandle limit # You can improve performance by caching filehandles sub append_data { my ($acct, $line) = @_; open(my $fh, '>>', "$acct.txt") or die "Unable to open '$acct.txt' + for appending: $!\n"; print $fh $line; }
#!/usr/bin/perl use strict; use warnings; my (%seen, %fh); while (<DATA>) { chomp; my ($acct, $data) = $_ =~ m{^(\d\d)(.*)}; next if $seen{$acct}{$data}++; append_data($acct, $_, \%fh); } # Improved performance by caching filehandles sub append_data { my ($acct, $line, $fh) = @_; if (! $fh->{$acct}) { open($fh->{$acct}, '>>', "$acct.txt") or die "Unable to open ' +$acct.txt' for appending: $!\n"; } print { $fh->{$acct} } $line; }
Cheers - L~R
|
---|