473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help - Counting text - Associative Array?

Greetings. I am trying to do something which should elementary for
Perl, but I have only been able to find bits and pieces on it. 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";

}

print "\nDONE.\n";

close(FILE);
Jul 19 '05 #1
7 3343
On Fri, 19 Mar 2004 at 16:59 GMT, Sam Lowry <mr******@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

Jul 19 '05 #2
Roel van der Steen <ro*******@st2x.net> wrote in message news:<sl**********************@localhost.localdoma in>...
On Fri, 19 Mar 2004 at 16:59 GMT, Sam Lowry <mr******@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


Dear Mr. van der Steen,

Thank you for your kind reply. Unfortunately, the strings are not
alone on their own lines in security.txt. I should have posted a
sample, thus:

3/15/2004,3:01:18 PM,Security,Success Audit,Object Access ,560,SERVER\
+refterm,SERVER,"Object Open:
Object Server: Security
Object Type: File
Object Name: C:\Program Files\Internet Explorer\IEXPLORE.EXE
New Handle ID: 536
Operation ID: {0,178316546}

3/15/2004,1:57:28 PM,Security,Success Audit,Object Access ,560,SERVER\
+Anon000,SERVER,"Object Open:
Object Server: Security
Object Type: File
Object Name: D:\crime\Reader\AcroRd32.exe
New Handle ID: 592
Operation ID: {0,177426959}

Your code shows me the logic and syntax, and I will study it to make
sure I understand what you did. The problem now is how to see the
array values in security.txt? Do I need to use regex or index
(mentioned elsewhere but which I know nothing about)?

- I just realized something: The context of the strings in
security.txt is always the same:

Object Name:[uniform space]$exe

If I include 'Object Name: ' with the C:\... as the complete value
in the array of my script it should work, no? Not the most elegant
solution but my eyes are already crossed after working on this having
had no formal training with Perl.

What is <the right way> to do this?

Thanks for reading.

Sam
Jul 19 '05 #3
On Sat, 20 Mar 2004 at 03:04 GMT, Sam Lowry <mr******@hotmail.com> wrote:
Roel van der Steen <ro*******@st2x.net> wrote in message news:<sl**********************@localhost.localdoma in>...
On Fri, 19 Mar 2004 at 16:59 GMT, Sam Lowry <mr******@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.

Thank you for your kind reply. Unfortunately, the strings are not
alone on their own lines in security.txt. I should have posted a
sample, thus:


Yes, that's what I already suspected. But you didn't ask that, did you?
The best thing would be to change the algorithm a bit, but this time
I go for the minimal changes. Add

(my $search = join '|', keys %exe) =~ s/\\/\\\\/g;

before "my %count;" and change "$count{$_}++ if exists $exe{$_};" to

$count{$1}++ if /($search)/o;

But it's not very elegant now.
Jul 19 '05 #4
Roel van der Steen <ro*******@st2x.net> wrote in message news:<sl**********************@195-86-124-242.dsl.easynet.nl>...
On Sat, 20 Mar 2004 at 03:04 GMT, Sam Lowry <mr******@hotmail.com> wrote:
Roel van der Steen <ro*******@st2x.net> wrote in message news:<sl**********************@localhost.localdoma in>...
On Fri, 19 Mar 2004 at 16:59 GMT, Sam Lowry <mr******@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.
Thank you for your kind reply. Unfortunately, the strings are not
alone on their own lines in security.txt. I should have posted a
sample, thus:


Yes, that's what I already suspected. But you didn't ask that, did you?


Yes, I did actually mention it originally, but I failed to give an
example and must take responsibility for being unclear. Het spijt me.
The best thing would be to change the algorithm a bit, but this time
I go for the minimal changes. Add

(my $search = join '|', keys %exe) =~ s/\\/\\\\/g;

before "my %count;" and change "$count{$_}++ if exists $exe{$_};" to

$count{$1}++ if /($search)/o;

But it's not very elegant now.

My main concern is that it works and also that I understand how it
works! I will get back to you after the weekend once I've had a
chance to study it/play with it.

Sincere thanks again for your efforts.

Sam
Jul 19 '05 #5
Dear Mr. van der Steen-

Your script works great. Would you please explain 2 lines to me:

(my $search = join '|', keys %exe) =~ s/\\/\\\\/g;

I think the text of the values belonging to the keys in the array is
being joined here but I don't understand the delimiting part =~
s/\\/\\\\/g. Please explain.

$count{$1}++ if /($search)/o;

Increase the count by 1 if the string is found in security.txt. How
does this work? What is if /($search)/o doing?

I want to fully understand your script so I can make 3 modifications:

1 - If a value is not found in security.txt I want $count=0. Right
now the result is blank and I get an error if warnings is turned on.

2 - The results are not sorted. The list is in a different order every
time despite foreach sort keys(%exe) in the print line.

3 - For neatness I'd like the counts to line up in a column rather
than just tab over from the key names.

Once I understand what you did I can try to achieve these things.

For your reference here's the whole script.

TIA Sam.

####Script###

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 $search = join '|', keys %exe) =~ s/\\/\\\\/g;

my %count;

open (FILE, "security.txt");
while (<FILE>) {
chomp;

$count{$1}++ if /($search)/o;

}
close FILE;

print "Name: $exe{$_}\t Count: $count{$_}\n" foreach sort
keys(%exe);
print "\nDONE.\n";
Jul 19 '05 #6
On Wed, 24 Mar 2004 at 01:14 GMT, Sam Lowry <mr******@hotmail.com> wrote:
Dear Mr. van der Steen-

Your script works great. Would you please explain 2 lines to me:

(my $search = join '|', keys %exe) =~ s/\\/\\\\/g;
This builds a part of the regex. The pipe wil function as an
OR operator. All backslashes need to be escaped because they're
special in a regex (that's the s/\\/\\\\/g).

$count{$1}++ if /($search)/o;

Increase the count by 1 if the string is found in security.txt. How
does this work? What is if /($search)/o doing?
The slashes delimit the regex, the parentheses capture the
matched string to the special variable $1. The "o" modifier
tells the Perl compiler that this regex is not expected to
change during program execution (despite the fact that there
is a variable in the regex) and that the regex only needs to
be compiled once.

I want to fully understand your script so I can make 3 modifications:

1 - If a value is not found in security.txt I want $count=0. Right
now the result is blank and I get an error if warnings is turned on.
I'd guess you need to initialise the variables then. Now I
look at my own little program again, I indeed see a problem
there. There are two possibilities: print out all the strings
that is searched for, even if they are not found, or only print
the strings that have a count greater than zero. My sample is
just in between -- and indeed buggy.

2 - The results are not sorted. The list is in a different order every
time despite foreach sort keys(%exe) in the print line.
This is also a bug. The sort is on the keys of %exe, so that
would be on the full path names, instead of the shorthand names.

3 - For neatness I'd like the counts to line up in a column rather
than just tab over from the key names.
OK. Use Anno Siegel's Text::Table for that. It's not part of
the standard Perl installation, but if you have ActivePerl,
type "ppm install Text-Table" on the command prompt to install
it.

Once I understand what you did I can try to achieve these things.

For your reference here's the whole script.

TIA Sam.


As a 3rd try I propose something like this:
#!/usr/bin/perl
use strict;
use warnings;
use Text::Table;

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

my %exe = (
Catalog => 'C:\Program Files\Internet Explorer\IEXPLORE.EXE',
Crime => 'D:\crime\Reader\AcroRd32.exe',
Foo => 'Bar',
);
my %count = map {$_, 0} keys %exe;

open FILE, "security.txt" or die $!;
while (<FILE>) {
while (my ($key, $value) = each %exe) {
$count{$key}++ if index($_, $value) > 0;
}
}
close FILE;

my $table = Text::Table->new(\'| ', 'Name', \' | ', 'Count', \' |');
$table->add($_, $count{$_}) foreach sort keys %count;
print $table->rule('-', '+'),
$table->title,
$table->rule('-', '+'),
$table->body,
$table->rule('-', '+');

print "\nDONE.\n";

__END__
Cheers, Roel
Jul 19 '05 #7
Roel van der Steen <ro*******@st2x.net> wrote in message news:<sl**********************@195-86-124-242.dsl.easynet.nl>...
On Wed, 24 Mar 2004 at 01:14 GMT, Sam Lowry <mr******@hotmail.com> wrote:
Dear Mr. van der Steen-

Your script works great. Would you please explain 2 lines to me:

(my $search = join '|', keys %exe) =~ s/\\/\\\\/g;


This builds a part of the regex. The pipe wil function as an
OR operator. All backslashes need to be escaped because they're
special in a regex (that's the s/\\/\\\\/g).

Yeah, but you forgot to quote the .

This is more effectively and more readably done as

my $search = join '|', map { quotemeta } keys %exe;

$count{$1}++ if /($search)/o;

Increase the count by 1 if the string is found in security.txt. How
does this work? What is if /($search)/o doing?


The "o" modifier
tells the Perl compiler that this regex is not expected to
change during program execution (despite the fact that there
is a variable in the regex) and that the regex only needs to
be compiled once.

The /o qualifier is dangerous - one day you may extend your program
such that $searh does change.

Much better to get into the habit of simply explicitly taking the
regex compliation outside the loop.

$search = qr/($search)/;

Note that IIRC even if you don't use qr// or /o the regex will still
only be compiled once so long as $search doesn't change.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bruce | last post by:
I don't quite understand this. $result is the result of a mysql_query. $requests = array(); while ($request = mysql_fetch_array($result)) { print("<p>"); print_r($request);...
3
by: sudeep | last post by:
C++ Programming Language Assignment 1 Associative Array Class Problem Statement Associative arrays are a useful feature found in many languages particularly in those that are interpretive and...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
4
by: mk834tt | last post by:
When you use an object as an assc array as in: var aa = new Array(); aa = 2.50; aa = 3.50; .... Is there a way to determine the number of entries? So far I count this way: for (var x in...
4
by: rsaharia | last post by:
Hello All, I need help with this particular .pl file I picked up from http://www.veritools-usa.com/xnf2vhdl.htm What it's supposed to do is really convert an xnf file to a vhdl file. I need it for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.