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

Good morning Monks!
Help.....
My goal here is to get an input from a variable like: my $test = "Joe's Store"; as
an example and print its name first and how many times it showed in the data file like:
Joe's Store N1:
Log in activity: 4
DI Activity: 3
DX Activity: 1
I am trying to log every transaction an user do in my program.
The code below does sort of what I want but it doesn't do by item,
there is a better way of doing this? I sure it is, can someone help me with that?
Thanks a lot!!!!
The code:
my $test = "Joe's Store"; my $log_file = "c:\log.txt"; open(LOGFILE, "$log_file") or die("Could not open log file."); while(<LOGFILE>){ my ($item) = $1 if /(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*? +)\*(.*?)$/; next if $seen{$item}; print "$item<br>"; print "$seen{$1}"; $seen{$item}++; } ____DATA__FILE Joe's Store N1*002113*pw12613x*DI*4/15/2004*11:58:46*1510043*1 Joe's Store N2*002113*pw12613x*DI*4/15/2004*11:58:46*1510043*1 Joe's Store N1*002113*pw12613x*DI*4/15/2004*11:58:46*1510043*1 Joe's Store N1*002113*pw12613x*DI*4/15/2004*11:58:46*1510043*1 Joe's Store N5*002113*pw12613x*DI*4/15/2004*11:58:46*1510043*1 Rental Company N1*002113*pw126513x*DI*4/15/2004*11:58:46*1510043*1 Joe's Store N6*002113*pw126513x*DI*4/15/2004*11:58:46*1510043*1 Joe's Store N1*002113*pw126513x*DX*4/15/2004*11:58:46*1510043*1 Joe's Store N8*002113*pw126513x*DI*4/15/2004*11:58:46*1510043*1 Joe's Store N9*002113*pw126513x*DI*4/15/2004*11:58:46*1510043*1 Rental Company N1*002113*pw126513x*DI*4/15/2004*11:58:46*1510043*1 Rental Company N2*002113*pw126513x*DI*4/15/2004*11:58:46*1510043*1 Travel X*003443*pw126513x*DI*4/15/2004*11:58:46*1510043*1 ___END_DATA__FILE

Replies are listed 'Best First'.
Re: Uniqueness in Text Data File Help
by fizbin (Chaplain) on Apr 19, 2004 at 13:13 UTC

    First off, you don't say that you want data in HTML, but your code seems to be trying that. I'll go with the assumption that you do not want you data in HTML, and it's relatively easy to convert the result to HTML if that's what you want.

    The code you posted suffers from the problem that you're generating output with each line, where what you really want is a summary. This means that you should just accumulate your results until you've read the entire file, and then form your output.

    Here's a minimal reworking of your code:

    my $test = "Joe's Store"; my $log_file = "c:\log.txt"; open(LOGFILE, "< $log_file") or die("Could not open log file."); my %seen = (); my %typeseen = (); # read it in while(<LOGFILE>) { # we might be better with a split() here next unless /(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*? +)$/; my ($item, $logintype) = ($1, $4); $seen{$item}++; $typeseen{$item}{$logintype}++; } # dump it out foreach my $item (sort keys %seen) { print "$item:\n"; print "\tLog in activity: ", $seen{$item}, "\n"; foreach my $type (sort keys %{$typeseen{$item}}) { print "\t", $type, " activity: ", $typeseen{$item}{$type}, "\n"; } }

    Incidentally, if this is a standalone script, you might want to consider setting $test from the command line.

      oh...i think i get you....you don't even need perl for that (although you easily could)....On any decent machine (unix/linux or a windows machine with cygwin installed)
      grep "Joe's Store" your_filename | wc
      ..or in perl:
      while($line=<>) { chomp $line; $a++ if($line =~ /Joe's Store/); } print "Joe's Store occured $a times\n";
        Actually if I just replace the $1 with the value of $test, I get the results I'm looking for, thank you!
      Hi as we see my $test = "Joe's Store"; is the item I want to generate the summary for, and the code you wrote it's good, but still does sort of what my code does, printing the summary for everything found on $1 or on the first element after reading the data file. I just want to find all that information for whatever value coming from the $test variable. But thanks anyway!!