Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Match and Count

Reply
Thread Tools

Match and Count

 
 
sidel
Guest
Posts: n/a
 
      12-21-2003
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


 
Reply With Quote
 
 
 
 
Roy Johnson
Guest
Posts: n/a
 
      12-22-2003
"sidel" <(E-Mail Removed)> wrote in message news:<gUkFb.13724$(E-Mail Removed) 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?
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      12-22-2003
In article <gUkFb.13724$(E-Mail Removed) k.net>,
sidel <(E-Mail Removed)> 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.
 
Reply With Quote
 
Michael Capone
Guest
Posts: n/a
 
      12-22-2003
"sidel" <(E-Mail Removed)> wrote in message news:<gUkFb.13724$(E-Mail Removed) 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";
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to match and count Ruby Newbee Ruby 7 11-23-2009 08:33 AM
creating a pattern using a previous match and a count of the numberof '('s in it me Python 0 01-27-2009 12:02 PM
How get cell count and match total column value Rahul Perl Misc 6 03-12-2007 09:06 AM
Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from? Kaimuri MCSD 3 12-29-2004 06:38 PM
I am adding a new row to the datagrid dynamically but if i use the Count property of Item it is not showing the count of the new rows being added Praveen Balanagendra via .NET 247 ASP .Net 2 06-06-2004 07:16 AM



Advertisments