Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count number of lines that matches

Status
Not open for further replies.

preethib

Programmer
Jul 20, 2000
39
IN
Hi

I have been reading your newsletters for some time. I would like to know some information on counting the repeating lines in a file. I currently have a script that grabs all URL address to a file, what I am trying to find is count the no. of times the URLs are found in that output file.

Any help will be appreciated.

Preethi

Below is my script:


use strict;
#use IO::Socket;

open (READ, "sample_scoff.txt") || die " cannot open output file \n";
my @scon_rd = <READ>;
open (WRITE, &quot;>scon_wr.txt&quot;) || die &quot; cannot open output file \n&quot;;
foreach my $line (@scon_rd) {
if ($line =~ m/^\d+/m )
{
my $num_line = $line;

#print &quot;$num_line&quot;;
if ($num_line =~ m/\s(http.*?)\s/mg)
{
print &quot;$1 \n\n&quot;;
}
}
close (READ);
}
open (ATon, &quot;scon_wr.txt&quot;) || die &quot; cannot open scon write file \n&quot;;
my @ATwr = <ATon>;
.................??????????

close (WRITE);
close (ATon);


 
The easiest (and probably least efficient) way is to read in one file to an array (probably the shortest file) then in a while(<filehandle>) loop on the other file, create a foreach loop on the array and use the $_ eq $arrayelement comparison to check for matches.

Or something along those lines.

brendanc@icehouse.net
 
Crap. I didn't read that right... Ignore that last post. Sheesh, I'm tired.

brendanc@icehouse.net
 
Okay. Similar logic to solve the task. Use multi-dimensional arrays or objects. On each individual line, create a new instance of the package or element in the array with a sub-element or member variable ($)count. Use a foreach or for loop to traverse through the array each time and if the URL already exists, increment $count. If you go the package route, you'll still need to maintain an array with references to each instance.

Three posts later, it's time for bed.

Good luck,

brendanc@icehouse.net
 
I won't try to write the whole program for you, I don't have the time. But here's a hint. The easiest way to count the number of times some value occurs is to use it as a hash key and increment the value. for example:
Code:
$counts{$string}++;
[\codeThen just print the value for each of the keys in the hash.
 
I seem to be having a problem typing TGML :~/

here's what it should have looked like:

Code:
$counts{$string}++;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top