Much as this smacks of a piece of University homework, I have made a solution anyway ;)
I came up with a rather cheeky solution to this. As it is possible for a company to access different files, the data structure warranted being a hash (of company names) against references another hash (of file names) for each company. However, this is a lot of effort.
I opted for forming a key to a single hash out of a company name and file name (separated by a comma) and keeping a count to the occurence of each of these. Then having read the data file, when the time came to output the results, I split the hash-key on the comma to give two separate scalars...
use strict;
# Declare vars
my @columns = ();
my %hash = ();
my $key = "";
# Open data file
open INFILE, "data.tmp";
# Process file line by line
while (<INFILE>) {
# Split on comma delimiters
@columns = split(/,/, $_);
# Store a number in a hash
$key = $columns[3] . "," . $columns[4];
if (defined($hash{$key})) {
$hash{$key} = $hash{$key} + 1;
} else {
$hash{$key} = 1;
}
}
# Output the hash
foreach $key (keys %hash) {
@columns = split(/,/, $key);
print "$columns[0] has accessed $columns[1] $hash{$key} time(s).\n
+";
}
This code expects the data provided in the question to be in the file data.tmp in the same dir as the script. It produces the following output as req'd:
The Company Inc. has accessed pw_p1 1 time(s).
MIT has accessed pw_p1 2 time(s).
My Agency Inc. has accessed pw_p1 1 time(s).
My Agency Inc. has accessed pw_p2 1 time(s).
Once again, I hope I haven't done ur homework for you! Arun |