To take your points in order:

  1. I put the file inside the script to keep everything together. Another way of doing this would be to place the data at the end of the script after a __END__ or __DATA__ tag and read the DATA filehandle that the interpreter opens for you. However, I wanted to show you how to use the three-argument form of open which is considered best practice these days. Just substitute your variable containing your file to be read for the \ <<EOD ...

  2. I just put the line numbers in the data to show that the script was giving the "right" answers. Having them there did not affect how the script ran.

  3. Put something like my ( $file, @phrases ) = @ARGV; at the top of your script so that you don't have to worry how many phrases are being sought.

  4. If you are calling your script from the command line with a file and a series of phrases then I imagine you will enclose each phrase in single-quotes. To avoid the problem where the phrase might contain regex metacharacters change the line compiling the regex to my $rxPhrase = qr{\Q$phrase\E};

I'm not sure how you arrive at an answer of 45; did you mean to say $c = "total cpu sold:"?

Cheers,

JohnGG


In reply to Re^3: search pattern and arrays by johngg
in thread search pattern and arrays by mercuryshipz

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.