First of all, you are only matching a single digit. You want to change your regular expression from /(\d)/ to /(\d+)/, which matches one or more digits, and slurps up as many as it finds in a row. One issue you will have is that a file containing addresses, you are also going to get a count on apartment numbers, street numbers, and zip codes. If you know the format of your phone numbers for sure, you can expand your regexp to grab just the patterns you need. Feel free to ask the list for help with that.
Secondly, you are counting how many times any number appears in the file. You are correctly grabbing the digit(s) in your regexp by using the ()s, but then you aren't using $1 to store the data that you grab. You will want to define a hash in your variable declaration area at the top of your code "my %PhoneNumCounts = ();" instead of "my $match_cntr = 0;" Then the increment would be: if (!exists $PhoneNumCounts{$1}) { $PhoneNumCounts{$1}=1; } else { $PhoneNumCounts{$1}++; } You will then need to add a loop to print out the count for each phone number. I've gone ahead and updated your code as follows: #!/usr/bin/perl # I don't use #! because I'm on windows... use warnings; use strict; my %PhoneNumCounts = (); my $n = 0; open(FILE,"<dat.txt"); my @file_array = <FILE> ; for($n=0;$n<=$#file_array;$n++) { while($file_array[$n]=~/(\d+)/gxom) { # print $1; # commented out for cleaner output if (!exists $PhoneNumCounts{$1}) { $PhoneNumCounts{$1}=1; } else { $PhoneNumCounts{$1}++; } } }#end of for loop close(FILE); my $phoneNum; foreach $phoneNum (sort keys (%PhoneNumCounts)) { # sort is optional print "$PhoneNumCounts{$phoneNum} occurances of $phoneNum\n"; } This gives the following output on your dat.txt: 4 occurances of 0216339509 Disclaimers: I'm sure that others on this list can shorten my code, and probably make it more readable. There are likely other issues in your code that I haven't addressed, I just tried to answer your question(s). I'm sure that there is perldoc on hashes, but I can't find it. -----Original Message----- From: Bruce Ambraal [mailto:[EMAIL PROTECTED]] Sent: Friday, March 22, 2002 6:31 AM To: [EMAIL PROTECTED] Subject: Printing the number of times phone number appear in a textfile Hi all In the program below I am attempting to count the number of times that a phone number(purely digits) occurs in a file of text. I am not succeeding cause i end up counting all the digits of phone numbers that occurs. Could anyone help. ---------------------------------------------------------------------------- ------------------------------ dat.txt 0216339509 is my phone number 0216339509 also belong to my brother 0216339509 and 0216339509 from sister. ---------------------------------------------------------------------------- ----------------------------- Program: #!/usr/bin/perl -w my $match_cntr = 0; my $n = 0; open(FILE,"<dat.txt"); my @file_array = <FILE> ; for($n=0;$n<=$#file_array;$n++) { while($file_array[$n]=~/(\d)/gxom){ print $1; $match_cntr++; } }#end of for loop print "NUMBERS COUNTED = ",$match_cntr,"\n"; close(FILE); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]