Match and Count

Discussion in 'Perl' started by sidel, Dec 21, 2003.

  1. sidel

    sidel Guest

    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
     
    sidel, Dec 21, 2003
    #1
    1. Advertising

  2. sidel

    Roy Johnson Guest

    "sidel" <> wrote in message news:<gUkFb.13724$>...
    > 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?
     
    Roy Johnson, Dec 22, 2003
    #2
    1. Advertising

  3. sidel

    Jim Gibson Guest

    In article <gUkFb.13724$>,
    sidel <> 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.
     
    Jim Gibson, Dec 22, 2003
    #3
  4. "sidel" <> wrote in message news:<gUkFb.13724$>...
    > 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";
    }
     
    Michael Capone, Dec 22, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    3
    Views:
    1,026
    David Carlisle
    Feb 16, 2005
  2. me
    Replies:
    0
    Views:
    267
  3. efelnavarro09
    Replies:
    2
    Views:
    968
    efelnavarro09
    Jan 26, 2011
  4. Ruby Newbee

    How to match and count

    Ruby Newbee, Nov 22, 2009, in forum: Ruby
    Replies:
    7
    Views:
    106
    Robert Klemme
    Nov 23, 2009
  5. Rahul
    Replies:
    6
    Views:
    168
    Rahul
    Mar 12, 2007
Loading...

Share This Page