in reply to Uniqueness in Text Data File Help

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.

Replies are listed 'Best First'.
Re: Re: Uniqueness in Text Data File Help
by aquarium (Curate) on Apr 19, 2004 at 13:52 UTC
    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!
Re: Re: Uniqueness in Text Data File Help
by Anonymous Monk on Apr 19, 2004 at 13:35 UTC
    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!!