469,903 Members | 1,705 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Match and Count

Hello,

I have a csv with name, address, city, state, zip (XXXXX-XXXX). I am hoping
to group all the similiar zip codes and produce a count on each group. My
match so far is m/^\d{5}-\d{4}$/ A match by the first five is ok, but how
would I roll through each one? (for example 90210, 90211, 90213 ...)

thanks
JS
Jul 19 '05 #1
3 5997
"sidel" <si****@hotmail.com> wrote in message news:<gU*******************@newsread1.news.pas.ear thlink.net>...
A match by the first five is ok, but how
would I roll through each one? (for example 90210, 90211, 90213 ...)


1. You'll probably get more responses at comp.lang.perl.misc or
perl.beginniers. This newsgroup does not exist in the official list.
2. What do you mean by the question I've quoted above?
Jul 19 '05 #2
In article <gU*******************@newsread1.news.pas.earthlin k.net>,
sidel <si****@hotmail.com> wrote:
Hello,

I have a csv with name, address, city, state, zip (XXXXX-XXXX). I am hoping
to group all the similiar zip codes and produce a count on each group. My
match so far is m/^\d{5}-\d{4}$/ A match by the first five is ok, but how
would I roll through each one? (for example 90210, 90211, 90213 ...)

thanks
JS


Use parentheses to capture the 5-digit zip code, then a hash to count
the entries (untested):

my %count;
while(...) # whatever loop you have
if( /^(\d{5})-\d{4}$/ ) {
$count{$1}++;
}
}

foreach my $zip ( sort keys(%count) ) {
print "$zip: $count{$zip}\n";
}

Two suggestions:
1. It is best to post a complete, minimal script demonstrating the
problem.
2. This newsgroup is defunct. Try comp.lang.perl.misc in the future.
Jul 19 '05 #3
"sidel" <si****@hotmail.com> wrote in message news:<gU*******************@newsread1.news.pas.ear thlink.net>...
Hello,

I have a csv with name, address, city, state, zip (XXXXX-XXXX). I am hoping
to group all the similiar zip codes and produce a count on each group. My
match so far is m/^\d{5}-\d{4}$/ A match by the first five is ok, but how
would I roll through each one? (for example 90210, 90211, 90213 ...)

thanks
JS


Is this what you're looking for?

open CSVFILE, "my.csv";
while(<CSVFILE>)
{
chomp;
($name, $address, $city, $state, $zip) = split /,/;
$zip =~ m/^(\d{5})-\d{4}$/ ;
$zipcounts{$1}++;
}
close CSVFILE;

foreach $zip (sort keys %zipcounts)
{
print "Zip code $zip has $zipcounts{$zip} entries\n";
}
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by cpnet | last post: by
19 posts views Thread by konrad Krupa | last post: by
reply views Thread by Malcolm Dew-Jones | last post: by
3 posts views Thread by Happy Face | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.