develooper Front page | perl.beginners | Postings from March 2002

Printing the number of times phone number appear in a textfile

Thread Next
From:
Bruce Ambraal
Date:
March 22, 2002 06:33
Subject:
Printing the number of times phone number appear in a textfile
Message ID:
sc9b5c2e.080@cct.org.za
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);


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About