Re: Help - Counting text - Associative Array?

From: Roel van der Steen (roel-perl_at_st2x.net)
Date: 03/19/04

  • Next message: Roel van der Steen: "Re: What directory perl script is being run from? - followup"
    Date: 19 Mar 2004 17:30:59 GMT
    
    

    On Fri, 19 Mar 2004 at 16:59 GMT, Sam Lowry <mr_lowry@hotmail.com> wrote:
    > direction.
    >
    > I want to count several strings of text in a file (security.txt) and
    > output the counts with a brief description of each.

    <OP's attempt snipped>

    Not too bad after all. But this works:

    #!/usr/bin/perl
    use strict;
    use warnings;

    print "\nSEARCHING...\n\n";

    my %exe = (
      'C:\Program Files\Internet Explorer\IEXPLORE.EXE' => 'Catalog',
      'D:\crime\Reader\AcroRd32.exe' => 'Crime',
    );
    my %count;

    open (FILE, "security.txt");
    while (<FILE>) {
      chomp;
      $count{$_}++ if exists $exe{$_};
    }
    close FILE;

    print "Name: $exe{$_}\t Count: $count{$_}\n" foreach sort keys %exe;
    print "\nDONE.\n";

    __END__

    security.txt would need to contain something like:

    C:\Program Files\Internet Explorer\IEXPLORE.EXE
    D:\crime\Reader\AcroRd32.exe
    D:\crime\Reader\AcroRd32.exe
    D:\crime\Reader\AcroRd32.exe


  • Next message: Roel van der Steen: "Re: What directory perl script is being run from? - followup"

    Relevant Pages

    • Re: Initializing an array comprised of very long strings
      ... them live in a single header file (escaped out the wazoo to get the ... Luckily gcc supports longer strings so the warnings are just ...
      (comp.lang.c)
    • Re: Grep through a log file
      ... but the strings in $culist don't appear unmodified in the log ... chomp $culist; ... Next, grep(). ... I tried your script. ...
      (perl.beginners)
    • Re: MAP question
      ... You could give an example of the contents of @_tem. ... as "perldoc -f chomp" will tell you. ... to do the chomping already before the strings went into the array. ... You could chomp all the strings in the list, the doc says, so we can do ...
      (comp.lang.perl.misc)
    • Re: sets
      ... >I want to be able to form a list of strings where duplicates may occur but ... Well they are not for that purpose but they can be used for that. ... It all depends what those warnings are. ...
      (alt.comp.lang.learn.c-cpp)
    • Re: need help with prog. logic
      ... You should always enable warnings and strict when developing Perl code. ... Dots are not special in strings, so there is no need to backslash them. ... That is testing for numerical equality. ...
      (comp.lang.perl.misc)