First, use \Q$domain\E instead of $domain in regexps, so you don't have to escape the contents of $domain. Otherwise, "foo.com" would match "foodcom".
# If the domain starts the line
/^\Q$domain\E\b/
# If the domain can be anywhere in the line
/\b\Q$domain\E\b/
'\b' indicates a word boundary.
Update: As an aside, @DISKD = <inputfile> won't work as expected, since you're trying to read the entire file every pass of the loop, without seek()ing to the top of the file. Move that line outside of the loop:
@DISKD = <inputfile>;
foreach $domain (@domainlist) {
@value = grep { /^\Q$domain\E\b/ } @DISKD;
for $value (@value) {
@data = split /\s+/, $value;
$sum += $data[$#data];
}
}
Update 2: Something like this would be more efficient, however:
# Calculate the sum for every domain.
@DISKD = <inputfile>;
foreach (@DISKD) {
@data = split(/\s+/, $_);
$sum{$data[0]} += $data[1];
}
# Filter out the domains we don't want.
# We can even skip this step if we don't care
# if %sum has more domains than @domainlist.
%domainlist = map { $_ => 1 } @domainlist;
foreach $domain (keys(%sum)) {
delete $sum{$domain} unless $domainlist{$domain};
}
|