http://qs321.pair.com?node_id=168239

A_CAR11 has asked for the wisdom of the Perl Monks concerning the following question:

16:6:28,4/13/2002,user 1,MIT,pw_p1,
16:6:35,4/13/2002,user 1,MIT,pw_p1,D
16:30:7,4/13/2002,user 2,My Agency Inc.,pw_p1,
16:30:32,4/13/2002,user 2,My Agency Inc.,pw_p2,
16:30:52,4/13/2002,user 3,The Company Inc.,pw_p1,

How many times "MIT" has accessed "pw_p1". I have to print something like:
MIT has accessed pw_b1 2 times.
The same analysis, for the others. Thank you for your help!!!
  • Comment on How to extract from the array...exemple txt file:

Replies are listed 'Best First'.
Re: How to extract from the array...exemple txt file:
by Aristotle (Chancellor) on May 22, 2002 at 00:17 UTC
    #!/usr/bin/perl -naF/,/ $seen{$F[3]}->{$F[4]}++ if defined $F[3] and defined $F[4]; END { while(($k,$v) = each %seen) { print "$k has accessed $k2 $v2 tim +es.\n" while ($k2,$v2) = each %$v } }
    ^_^

    Update: The folks in the chatterbox made me itchy. Golfing time anyone? (Mostly I just didn't want to be beaten out by use of the propositions I mentioned in CB myself.. ^_^)
    #!/usr/bin/perl -naF/,/ $seen{"@F[4,3]"}++;END{@u;print"@u has accessed $s $t times.\n"while($ +_,$t)=each%seen,($s,@u)=split}
    ____________
    Makeshifts last the longest.
      Lol, just a little hint on how to use this one.
      (You know, for those not as smart like me):

      script.pl info.dat OR perl -naF/,/ script.pl info.dat

      where script.pl => Name of The Script
      where info.dat => Name of File Containing Data

      The second way of executing the script in required on Win32 machines :)

I Hope This Isn't A Piece of Homework....
by arunhorne (Pilgrim) on May 21, 2002 at 23:16 UTC

    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

      Isn't this a good place for a hash of hashes (HoH)?

      Christopher E. Stith
      use coffee;
Extracting via Hash of Hashes
by mt2k (Hermit) on May 21, 2002 at 23:59 UTC
    Okay, here is something I whipped up. Because I see a ",D" at the end of the first line, I added a bit to the regex I used... You may have to edit the regex depending what the entire line looks like.

    #!c:/perl/bin/perl -w use strict; #Declare variables use vars qw(%hash $key $value); #Open the file with data in it open FILE, "info.log"; #Create a hash of hashes of companies/files while (<FILE>) { $hash{$4}{$5}++ while (/(.*?),(.*?),(.*?),(.*?),(.*?),(.*?)+/g); } #Show the results foreach my $company (keys %hash) { print "Company: $company\n"; print "\t$key accessed $value times.\n" while (($key,$value) = ea +ch %{$hash{$company}}); print "\n"; }


    Update: If you hate regexes (or simply to be safer), you could always split. This would replace the one while loop with:

    while (<FILE>) { my @a = split /,/; $hash{$a[3]}{$a[4]}++; }
Re: How to extract from the array...exemple txt file:
by Marza (Vicar) on May 21, 2002 at 20:28 UTC

    Hmmmm Homework?

    I would suggest taking a look at split and hashing for this example

    Originally posted as a Categorized Answer.

Re: How to extract from the array...exemple txt file:
by A_CAR11 (Initiate) on May 21, 2002 at 21:33 UTC
    Not a home work just used MIT as an exemple and to make the question more clear. Tks!

    Originally posted as a Categorized Answer.