It occurred to me that I could optimise this by delivering to the most frequent addresses first: should save on nslookups, and frequently-appearing hostnames are more likely to be valid than the "go@away.no.spam" variety. About 80% of the list should be processed before I run into the single-user hostnames. The most frequently occurring hostname is on the same host as the mailing program, so this 40% of the list should be delivered almost immediately. (BTW, the host doesn't use sendmail - I don't get to choose the MTA.)
Here's the outline of the code I produced, and I'd be grateful for any comments about how I could do this more efficiently. It's not bad atm, sorting the 50,000 addresses in ~ 3 secs, but I'm sure there's another and probably more efficient way to do it.
# unsorted addresses start off in the @addresses array. # sorted ones end up in the @recipients array. foreach my $address (@addresses) { my ($name, $host) = split(/\@/, $address); # various checks on the validity of name and host, snipped push @{$host}, "$name\@$host"; $frequency{$host}++; } foreach my $host (reverse sort { $frequency{$a} <=> $frequency{$b} } k +eys %frequency) { foreach my $address (@{$host}) { push @recipients, $address; } @{$host} = -1; }
So I'm naturally concerned about the wild proliferation of (often one-element) arrays, even though these are exterminated later. TIA.
In reply to Sorting a list by frequency of items by yojimbo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |