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}; }

In reply to Re: Regex how to by ikegami
in thread Regex how to update by frank_2k4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.