note
Limbic~Region
[Anonymous Monk],
<br />
If I have understood what you are trying to do, it boils down to these requirements:
<ul>
<li>A single file contains multiple lines.</li>
<li>Each line is comprised of an account and then data</li>
<li>The desired end state is for each account to be in its own file</li>
<li>Any duplicate data for an account should be ignored</li>
</ul>
<p>
Assuming the above is correct, here is how I would do it:
</p>
<c>
#!/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;
}
</c>
Here is how it would look if you know you will be safe caching file handles.
<c>
#!/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;
}
</c>
<div class="pmsig"><div class="pmsig-180961">
<p>
Cheers - [Limbic~Region|L~R]
</p>
</div></div>
1083028
1083028