473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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 6093
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Aaron C | last post by:
Hi, I'm trying to do an insert with the following statement: INSERT INTO user VALUES ( 'ag@ag.com','ag','Aaron','Chandler','','','La Mirada','CA',90638,714,'',''); and I'm getting the error...
3
by: Andy Fish | last post by:
Hi, I'm trying to use <xsl:number> to generate a sequence number of all nodes that match a particular pattern, e.g: <xsl:template match="foo"> <xsl:copy> <xsl:attribute name="id"><xsl:number...
4
by: Laszlo Csabi | last post by:
Hi Folks, Can someone explain me why I'm getting the following error? The error message I got is : "Parameter count does not match Parameter Value count."
4
by: cpnet | last post by:
I have a known binary value, and I want to match it to other binary values when a certain number of the same bits in both values are 1. For example, if I have the known value 0111, I want it to...
0
by: tania | last post by:
i have this table in my database: CREATE TABLE FILM( F_ID INT(5) NOT NULL AUTO_INCREMENT, F_TITLE VARCHAR(40) NOT NULL, DIRECTOR_FNAME VARCHAR(20) NOT NULL, DIRECTOR_LNAME VARCHAR(20) NOT NULL,...
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
16
bugboy
by: bugboy | last post by:
I'm running a query on a table with two foreign key columns- wID and dID which define a many to many relationship. I know the value of two different wID's ( wID=1, wID=2) and I need to find only the...
0
by: Malcolm Dew-Jones | last post by:
I have a test= that works but I would like to figure out how to move it into a match=. (Ok, that's unclear but pls keep reading.) Part of my XSLT file looks like this. <xsl:template...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.