Well, it's been seven hours since the OP was last seen here, so at this point I can take a crack at it for fun without feeling like I'm doing free work.

#!usr/bin/env perl use strict; use warnings; sub is_street {return shift =~ m/^\d+/;} sub is_postal {return shift =~ m/^\w+.+\d$/;} sub street_components { my $address = shift; if ($address =~ m/^ (.+) \s+APT\s+ # APT anchor (?:\(Range\s+)? # Range syntax ([\w\d]+(?:\s+-\s+[\w\d]+)?) # Apartment number \)? # Closing range syntax $/x ) { return {street => $1, apartment => $2} } else { die "Street address match failure: <<$address>>\n"; } } sub postal_component {return shift} sub apartment_expand { my $apartment_range = shift; my ($low, $high) = split /\s*-\s*/, $apartment_range; return [$low] if !length($high); my ($low_num, $low_alpha ) = $low =~ m/^(\d+)(\w+)$/; my ($high_num, $high_alpha) = $high =~ m/^(\d+)(\w+)$/; my @return; foreach my $num ($low_num .. $high_num) { # Numeric +increment. foreach my $letter ($low_alpha .. $high_alpha) { # Alpha in +crement. push @return, "${num}${letter}"; } } return \@return; } my %record; while (my $line = <DATA>) { chomp $line; next unless length $line; $record{'addr'} = street_components($line) if is_street($line); $record{'postal'} = postal_component($line) if is_postal($line); if (exists $record{'addr'} && exists $record{'postal'}) { my $apartments = apartment_expand($record{'addr'}->{'apartment +'}); foreach my $apartment (@$apartments) { printf "%s, APT %s, %s\n" => $record{'addr'}->{'street'}, $apartment, $record{'postal'}; } undef %record; } } __DATA__ 432 10TH ST APT (Range 2A - 2B) BROOKLYN NY 10598-6601 432 10TH ST APT (Range 3A - 3B) BROOKLYN NY 10598-6601 432 10TH ST APT (Range 4A - 4B) BROOKLYN NY 10598-6605 432 10TH ST APT (Range 5A - 5D) BROOKLYN NY 10598-6605 432 10TH ST APT 6A BROOKLYN NY 10598-6605

This produces the following output:

432 10TH ST, APT 2A, BROOKLYN NY 10598-6601 432 10TH ST, APT 2B, BROOKLYN NY 10598-6601 432 10TH ST, APT 3A, BROOKLYN NY 10598-6601 432 10TH ST, APT 3B, BROOKLYN NY 10598-6601 432 10TH ST, APT 4A, BROOKLYN NY 10598-6605 432 10TH ST, APT 4B, BROOKLYN NY 10598-6605 432 10TH ST, APT 5A, BROOKLYN NY 10598-6605 432 10TH ST, APT 5B, BROOKLYN NY 10598-6605 432 10TH ST, APT 5C, BROOKLYN NY 10598-6605 432 10TH ST, APT 5D, BROOKLYN NY 10598-6605 432 10TH ST, APT 6A, BROOKLYN NY 10598-6605

It's unfortunate that the data lacks a record separator; that means you have to keep track of what state you are in. If it mattered, I'd do more detection of getting out of sync by verifying we didn't get a city before getting an address.

If one were to use this for anything more than amusement they would quickly discover how fragile the address detection is, and that would lead to a realization of how unfortunate the input data format is.


Dave


In reply to Re: Extract data from txt file by davido
in thread Extract data from txt file by bulgin24

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.