Good Day,

I was looking at adding US zip code and city lookup to a program I'm writing, so I'm trying a quick test. I got the hard part to work (as far as I was concerned), but I'm having troubles with the simple stuff.

The following code simply passes the entered information to usps.gov and asks for the matches. It can handle either a five digit zip code, or a City, State pair.
use strict; use warnings; use LWP::UserAgent; print 'Enter Search: '; chomp( my $x = <STDIN> ); # Create a user agent object my $ua = new LWP::UserAgent; $ua->agent("AgentName/0.1 " . $ua->agent); # Create a request my $req = new HTTP::Request POST => 'http://www.usps.com/cgi-bin/zip4/ +ctystzip2'; $req->content_type('application/x-www-form-urlencoded'); $req->content('ctystzip='.$x); my $res = $ua->request($req); my @matches; my $data = $res->content; $data =~ /----------</g; if ($x =~ /^\s*\d{5}\s*/) { while ($data =~ /\G(br>((\w+\s)+\s*\w\w ))/ig) { push @matches, $2; } } else { while ($data =~ /\G(br>(\d{5}))/ig) { push @matches, $2; } } print 'Found ',$#matches+1,' matches:',"\n ", join("\n ", @matches), +"\n";
Here are the results of a few test cases
[duelafn@XYZZY bbdb]$ perl States.pl Enter Search: Mishawaka, IN Found 0 matches: [duelafn@XYZZY bbdb]$ perl States.pl Enter Search: Chicago, IL Found 0 matches: [duelafn@XYZZY bbdb]$ perl States.pl Enter Search: 84102 Found 1 matches: SALT LAKE CITY UT [duelafn@XYZZY bbdb]$ perl States.pl Enter Search: 12345 Found 1 matches: SCHENECTADY NY
The tests tell me that what's wrong is in the "else" case, but when I put a "print pos $data" in there it prints a reasonable position.

Thanks,
    Dean

In reply to specific /\G/g troubles by duelafn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.