It is with great reluctance (and hope) that I post such a basic problem.

So far I have only been able to find bits and pieces on this and when I put the bits together they do not work. Maybe I am going in the wrong direction.

I want to count several strings of text in a file (security.txt) and output the counts with a brief description of each. I have tried specifying the descriptions (name) and the strings (exe) in the array within the perl script. I can open the file to search ok, but the counting/output doesn't work. It seems to count every line in the file. The strings are not on their own lines in security.txt.

The goal is to see something like:

Name: Catalog Count:2 Name: Crime Count:1
Yes I am a newb, so if anyone can point me in the right direction I'd sure appreciate it. TIA, Sam
print "\nSEARCHING...\n"; open (FILE, "security.txt"); print "\n"; ###### Define names and their file paths ###### %exe = ( "Catalog", 'C:\Program Files\Internet Explorer\IEXPLORE.EXE', "Crime", 'D:\crime\Reader\AcroRd32.exe'); ###### Try to count the occurance of file paths ###### $count=0; while(<FILE>) { chomp; #if ($_ = (values %exe)) { $count++; } ###### print names and counts ####### foreach $key (keys %exe) { print "Name: $key\t Count: $count\n"; } close(FILE); print "\nDONE.\n";

In reply to Help - Counting text - Associative Array? by Anonymous Monk

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.