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


In reply to I Hope This Isn't A Piece of Homework.... by arunhorne
in thread How to extract from the array...exemple txt file: by A_CAR11

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.