CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
count of matches in a file

 



kumaichi
Deleted

May 24, 2001, 10:18 AM

Post #1 of 8 (6007 views)
count of matches in a file Can't Post

Hi,

I was wondering if it's possible to search a file (could grow large) and count how many times a certain string is found. For example, let's say I have the following file:

file.dat
John|Doe|123|Hello|World
Jane|Doe|123|Goodbye|World

Now, let's say I want to find out how many times Doe is in the file. Is there any way to do this?

Thanks for any help you can provide.



freddo
User

May 24, 2001, 12:24 PM

Post #2 of 8 (6004 views)
Re: count of matches in a file [In reply to] Can't Post

Hi Kumaichi,

you can use the following:

Code
$count = 0; 
open FILE, '< myfile';
while (<FILE>) {
$count++ if /Doe/;
}
close FILE;
print $count;

If you only need to check the second field, you can also use:

Code
$count = 0; 
open FILE, '< myfile';
while (<FILE>) {
$count++ if ( (split /\|/)[1] =~ /Doe/);
}
close FILE;
print $count;

I hope this helps,
freddo

;---
Real programmers´butcher dont understand when they just ask for 3735928559.


kumaichi
Deleted

May 24, 2001, 2:36 PM

Post #3 of 8 (6000 views)
Re: count of matches in a file [In reply to] Can't Post

freddo,

Thanks for your help, it seems to work great except for the case where I'm looking for let's say '6' and what I'm comparing to is '677'. This indicates it's a match when infact I would like it to match only '6', is this possible or am I asking for to much?

Thanks again,

kumaichi



freddo
User

May 24, 2001, 3:28 PM

Post #4 of 8 (5998 views)
Re: count of matches in a file [In reply to] Can't Post

It's possible for sure, use:

Code
$count = 0; 
$search = "Doe";

open FILE, '< myfile';
while (<FILE>) {
$count++ if ( (split /\|/)[1] =~ /^$search$/);
}
close FILE;
print $count;

just set $search to whatever you like.

If you want more info about regular expression; check perldoc perlre

freddo

;---
Real programmers´butcher dont understand when they just ask for 3735928559.


kumaichi
Deleted

May 24, 2001, 3:50 PM

Post #5 of 8 (5997 views)
Re: count of matches in a file [In reply to] Can't Post

freddo,

Thanks so much for your help, that did the trick :-).

This may not be the right place to ask this but I was wondering if there is any way to make it faster. For instance, I have a file that looks like this:

0|0|Parent Record|field2
1|0|Child Record|field2
2|0|Child Record|field2
3|2|Child of Child Record|field2

So, basically what I'm doing is, I'm getting each parent record and counting how many children it has, in the above case records 1 and 2 are children of 0 and record 3 is a child of record 2. What this means is, for every parent the file has to be opened again and I search for children and increment the count. Once the file is done I know how many children there are. The problem is, it's really slow to be parsing the entire file each time, especially because it will get bigger and means it will get exponentially slower.

Do you have any thoughts on this? I apologize if this is the wrong place to ask this question.

Again, thanks alot for your help today,

kumaichi



freddo
User

May 24, 2001, 4:31 PM

Post #6 of 8 (5994 views)
Re: count of matches in a file [In reply to] Can't Post

Hi again kumaichi,

You should perhaps make a new post for new questions, so people will look at it for the topic. Anyway, here is my quick and dirty solution (try to never do such awfull code, i just did it to give you the idea, it would probably be best to use an array of hashes)

Code
#!/usr/bin/perl 

open FILE, "< db.txt";
while (<FILE>) {
($ID, $PID, $PARENT_REC, $FIELD2) = split(/\|/);

$parent_rec[$ID] = $PARENT_REC;
$field2[$ID] = $FIELD2;
$count[$ID] = 0 unless $count[$ID];
$parent_id[$ID] = $PID if ($PID);
$count[$PID]+= 1 if ($PID);
}
close FILE;

for ($i=1; $i < @count; $i++) {
print "-"x40, "\n",
"I am ", $parent_rec[$i], " and have ", $count[$i], " child(s).\n";
print "I am also the child of ", $parent_rec[$parent_id[$i]], "\n"
if ($parent_rec[$parent_id[$i]]);
}


for the following db.txt file:

Code
1|0|Francois|field2 
2|1|Annette|field2
3|1|Francine|field2
4|3|Frederic|field2

Notice how i changed the id to keep the 0 to create a new person (people with $PID = 0 dont have any relation with others).

And this will output:

Code
---------------------------------------- 
I am Francois and have 2 child(s).
----------------------------------------
I am Annette and have 0 child(s).
I am also the child of Francois
----------------------------------------
I am Francine and have 1 child(s).
I am also the child of Francois
----------------------------------------
I am Frederic and have 0 child(s).
I am also the child of Francine

i hope it helps (to know my genealogy Smile )

freddo

;---
Real programmers´butcher dont understand when they just ask for 3735928559.

(This post was edited by freddo on May 24, 2001, 3:49 PM)


kumaichi
Deleted

May 24, 2001, 5:35 PM

Post #7 of 8 (5986 views)
Re: count of matches in a file [In reply to] Can't Post

freddo,

WOW, that is great, more than I could have hoped for. Thanks so much for taking time out of your day to help me with this. I will let you know how it goes.

Have a great day,

kumaichi



freddo
User

May 24, 2001, 7:33 PM

Post #8 of 8 (5980 views)
Re: count of matches in a file [In reply to] Can't Post

You´re welcome Kuma!

Happy to know it helped you Smile
(and, in fact, it´s taking time on my nights, it´s nearly 5am here Tongue )

freddo

;---
Real programmers´butcher dont understand when they just ask for 3735928559.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives